Skip to content

Commit

Permalink
Add spec and scripts for creating chocolatey package for crc
Browse files Browse the repository at this point in the history
the package currently performs the setup steps like installing hyper-v
creation of 'crc-users' group, adding current user to the
hyper-v administrators group. It doesn't install or setup the tray

it is a portable choco package and embedds the crc and crc-admin-helper-windows
binaries
  • Loading branch information
anjannath authored and openshift-merge-robot committed Jan 18, 2023
1 parent f8b21f8 commit ad5d477
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 4 deletions.
19 changes: 15 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ test:

GENERATED_RPM_FILES=packaging/rpm/crc.spec images/rpmbuild/Containerfile
spec: $(GENERATED_RPM_FILES)

test-rpmbuild: spec
${CONTAINER_RUNTIME} build -t test-rpmbuild-img -f images/rpmbuild/Containerfile .
${CONTAINER_RUNTIME} create --name test-rpmbuild test-rpmbuild-img
Expand All @@ -142,7 +142,7 @@ build_docs:

.PHONY: docs_serve
docs_serve: build_docs
${CONTAINER_RUNTIME} run -it -v $(CURDIR)/docs:/docs:Z --rm -p 8088:8088/tcp $(DOCS_BUILD_CONTAINER) docs_serve
${CONTAINER_RUNTIME} run -it -v $(CURDIR)/docs:/docs:Z --rm -p 8088:8088/tcp $(DOCS_BUILD_CONTAINER) docs_serve

.PHONY: docs_check_links
docs_check_links:
Expand Down Expand Up @@ -283,9 +283,9 @@ linux-release: clean lint linux-release-binary embed_crc_helpers gen_release_inf
@mkdir -p $(BUILD_DIR)/crc-linux-$(CRC_VERSION)-amd64
@cp LICENSE $(BUILD_DIR)/linux-amd64/crc $(BUILD_DIR)/crc-linux-$(CRC_VERSION)-amd64
tar cJSf $(RELEASE_DIR)/crc-linux-amd64.tar.xz -C $(BUILD_DIR) crc-linux-$(CRC_VERSION)-amd64 --owner=0 --group=0

@mv $(RELEASE_INFO) $(RELEASE_DIR)/$(RELEASE_INFO)

cd $(RELEASE_DIR) && sha256sum * > sha256sum.txt

.PHONY: embed_crc_helpers
Expand Down Expand Up @@ -383,3 +383,14 @@ $(BUILD_DIR)/windows-amd64/crc-windows-installer.zip: $(BUILD_DIR)/windows-amd64
rm -f $(HOST_BUILD_DIR)/split
pwsh -NoProfile -Command "cd $(HOST_BUILD_DIR); Compress-Archive -Path $(CABS_MSI) -DestinationPath crc-windows-installer.zip"
cd $(@D) && sha256sum $(@F)>$(@F).sha256sum

.PHONY: choco choco-clean
CHOCO_PKG_DIR = packaging/chocolatey/crc
choco: clean choco-clean $(BUILD_DIR)/windows-amd64/crc.exe $(HOST_BUILD_DIR)/crc-embedder $(CHOCO_PKG_DIR)/crc.nuspec
$(HOST_BUILD_DIR)/crc-embedder download --goos=windows --components=admin-helper $(CHOCO_PKG_DIR)/tools
cp $(BUILD_DIR)/windows-amd64/crc.exe $(CHOCO_PKG_DIR)/tools/crc.exe
cp LICENSE $(CHOCO_PKG_DIR)/tools/LICENSE.txt
cd $(CHOCO_PKG_DIR) && choco pack
choco-clean:
rm -f $(CHOCO_PKG_DIR)/*.nupkg
rm -f $(CHOCO_PKG_DIR)/tools/*.exe
4 changes: 4 additions & 0 deletions packaging/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
/darwin/*.pkg
/windows/msi/
/windows/product.wxs
/chocolatey/crc/*.nupkg
/chocolatey/crc/crc.nuspec
/chocolatey/crc/tools/*.exe
/chocolatey/crc/tools/LICENSE.txt
51 changes: 51 additions & 0 deletions packaging/chocolatey/crc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Chocolatey package for CRC (non GUI)

## Pre-requisite

The host machine needs to have [Chocolatey](https://chocolatey.org/) installed and should be able to build `crc`. Follow [chocolatey install guide](https://chocolatey.org/install) to install it.

## Steps to build the package

Build steps for the chocolatey package is incorporated in the `Makefile` under the `choco` target.

1. Run `make choco`:
```
PS> make choco
rm -rf /cygdrive/c/Users/anath/redhat/crc/docs/build
rm -f packaging/darwin/Distribution
rm -f packaging/darwin/Resources/welcome.html
rm -f packaging/darwin/scripts/postinstall
rm -rf packaging/darwin/root/
rm -rf packaging/windows/msi
rm -f out/windows-amd64/split
rm -f packaging/rpm/crc.spec images/rpmbuild/Containerfile
rm -rf out
rm -f C:\Users\anath\go/bin/crc
rm -rf release
GOARCH=amd64 GOOS=windows go build -tags "containers_image_openpgp" -ldflags="-X github.com/crc-org/crc/pkg/crc/version.crcVersion=2.12.0 -X github.com/crc-org/crc/pkg/crc/version.ocpVersion=4.11.18 -X github.com/crc-org/crc/pkg/crc/version.okdVersion=4.11.0-0.okd-2022-11-05-030711 -X github.com/crc-org/crc/pkg/crc/version.podmanVersion=4.2.0 -X github.com/crc-org/crc/pkg/crc/version.commitSha=40b730ce " -o out/windows-amd64/crc.exe ./cmd/crc
go build --tags="build" -ldflags="-X github.com/crc-org/crc/pkg/crc/version.crcVersion=2.12.0 -X github.com/crc-org/crc/pkg/crc/version.ocpVersion=4.11.18 -X github.com/crc-org/crc/pkg/crc/version.okdVersion=4.11.0-0.okd-2022-11-05-030711 -X github.com/crc-org/crc/pkg/crc/version.podmanVersion=4.2.0 -X github.com/crc-org/crc/pkg/crc/version.commitSha=40b730ce " -o out/windows-amd64/crc-embedder ./cmd/crc-embedder
out/windows-amd64/crc-embedder download --goos=windows packaging/chocolatey/crc/tools
0 B / 97.26 MiB [______________________________________________________________________________________________________________________________________________________________________________________] 0.00% ? p/s
cp out/windows-amd64/crc.exe packaging/chocolatey/crc/tools/crc.exe
cd packaging/chocolatey/crc && choco pack
Chocolatey v1.2.0
Attempting to build package from 'crc.nuspec'.
Successfully created package 'C:\Users\anath\redhat\crc\packaging\chocolatey\crc\crc.2.12.0.nupkg'
```

2. Above command will generated `crc.nupkg` in the `packaging/chocolatey/crc` folder

## Steps to push the package to chocolatey community repo

1. Create [account](https://community.chocolatey.org/account/Register) in the chocolatey community feed.
3. Copy the API Key from the [accounts page](https://community.chocolatey.org/account) (Click on the Show API Key button).
4. Associate your API key with community feed:
```
PS> choco apikey --key <account_api_key> --source https://push.chocolatey.org/
```
5. Push the package using:
```
PS> choco push crc.2.11.0.nupkg --source https://push.chocolatey.org/
```

> Note: Chocolatey has a moderation process and there might be a need to answer queries from the moderators, keep an eye on the [package queue](https://community.chocolatey.org/packages).
30 changes: 30 additions & 0 deletions packaging/chocolatey/crc/crc.nuspec.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>crc</id>
<version>__VERSION__</version>
<owners>https://github.com/crc-org</owners>
<title>CRC - Runs Containers</title>
<authors>anjannath</authors>
<projectUrl>https://crc.dev/blog/about</projectUrl>
<iconUrl>https://i.ibb.co/m0b6dsL/crc-upstream-logo.png</iconUrl>
<licenseUrl>https://github.com/crc-org/crc/blob/main/LICENSE</licenseUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<tags>crc ocp openshift openshiftlocal okd podman</tags>
<summary>CRC is a tool to run containers. It manages a local OpenShift 4.x cluster, OKD cluster or a Podman VM optimized for testing and development purposes.</summary>
<description>
## CRC - Runs Containers

CRC is a tool to run containers. It manages a local OpenShift 4.x cluster, OKD cluster or a Podman VM optimized for testing and development purposes.
Learn more and contribute on [GitHub](https://github.com/crc-org/crc).
</description>
<releaseNotes>https://github.com/crc-org/crc/releases/tag/v__VERSION__</releaseNotes>
<copyright>2022 Red Hat, Inc</copyright>
<packageSourceUrl>https://github.com/crc-org/crc/tree/main/packaging/chocolatey</packageSourceUrl>
<projectSourceUrl>https://github.com/crc-org/crc</projectSourceUrl>
<docsUrl>https://crc.dev/crc</docsUrl>
</metadata>
<files>
<file src="tools\**" target="tools" />
</files>
</package>
6 changes: 6 additions & 0 deletions packaging/chocolatey/crc/tools/VERIFICATION.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
VERIFICATION

Verification is intended to assist the Chocolatey moderators and community
in verifying that this package's contents are trustworthy.

This package is published by the CRC project itself.
18 changes: 18 additions & 0 deletions packaging/chocolatey/crc/tools/chocolateybeforemodify.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This runs in 0.9.10+ before upgrade and uninstall.
# Use this file to do things like stop services prior to upgrade or uninstall.
# NOTE: It is an anti-pattern to call chocolateyUninstall.ps1 from here. If you
# need to uninstall an MSI prior to upgrade, put the functionality in this
# file without calling the uninstall script. Make it idempotent in the
# uninstall script so that it doesn't fail when it is already uninstalled.
# NOTE: For upgrades - like the uninstall script, this script always runs from
# the currently installed version, not from the new upgraded package version.

$ErrorActionPreference = 'SilentlyContinue';

# stop crc-admin-helper service and remove it
Stop-Service -Name 'crcAdminHelper' -Force -Confirm:$false | Out-Null
sc.exe delete "crcAdminHelper" | Out-Null

# stop crc daemon scheduled task and remove it
Stop-ScheduledTask -TaskName 'crcDaemon' | Out-Null
Unregister-ScheduledTask -TaskName 'crcDaemon' -Confirm:$false | Out-Null
18 changes: 18 additions & 0 deletions packaging/chocolatey/crc/tools/chocolateyinstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$ErrorActionPreference = 'Stop'; # stop on all errors
$toolsDir = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
$setupScript = Join-Path "$toolsDir" "crcprerequisitesetup.ps1"

Import-Module $setupScript

# generate ignore file for crc-admin-helper-windows.exe to avoid shim generation and addintion to PATH
New-Item "$toolsDir\crc-admin-helper-windows.exe.ignore" -ItemType File -Force | Out-Null

if (Test-ProcessAdminRights) {
New-CrcGroup
Install-Hyperv
Install-AdminHelper -AdminHelperPath "$toolsDir\crc-admin-helper-windows.exe"
New-VsockGuestCommunicationRegistry
} else {
Write-Host "CRC needs administrator privileges to enable Hyper-V, create new group and add current user to needed groups, please run the installation as administrator"
Exit 1
}
8 changes: 8 additions & 0 deletions packaging/chocolatey/crc/tools/chocolateyuninstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$ErrorActionPreference = 'SilentlyContinue';
$toolsDir = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
$setupScript = Join-Path "$toolsDir" "crcprerequisitesetup.ps1"

Import-Module $setupScript

# remove crc-users group
Remove-LocalGroup -Name 'crc-users' -Confirm:$false | Out-Null
66 changes: 66 additions & 0 deletions packaging/chocolatey/crc/tools/crcprerequisitesetup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
$CrcGroupName = 'crc-users'
$CrcAdminHelperServiceName = 'crcAdminHelper'
$VsockGuestCommunicationRegistryPath = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices\00000400-FACB-11E6-BD58-64006A7986D3"

function New-CrcGroup {
$crcgrp = Get-LocalGroup -Name $CrcGroupName -ErrorAction SilentlyContinue
if ($crcgrp) {
Write-Host "crc-users group already exists"
} else {
New-LocalGroup -Name $CrcGroupName -Description 'Group for Red Hat OpenShift Local users' | Out-Null
}
}

function Install-Hyperv {
$osProductType = Get-ComputerInfo | Select-Object -ExpandProperty OSProductType | Out-String -Stream | Where-Object { $_.Trim().Length -gt 0 }
switch ($osProductType)
{
"WorkStation" {
# check if hyperv is already enabled
$enabled = (Get-WindowsOptionalFeature -FeatureName "Microsoft-Hyper-V" -online | Select-Object -ExpandProperty State).ToString().Equals("Enabled")
if ($enabled) {
Write-Host "Hyper-V is already enabled"
} else {
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart -ErrorAction SilentlyContinue | Out-Null
}
}
"Server" {
$enabled = (Get-WindowsFeature -Name "Hyper-V" | Select-Object -ExpandProperty InstallState).ToString().Equals("Installed")
if ($enabled) {
Write-Host "Hyper-V is already enabled"
} else {
Install-WindowsFeature -Name Hyper-V -IncludeManagementTools -ErrorAction SilentlyContinue | Out-Null
}
}
}
}

function Install-AdminHelper([string]$AdminHelperPath) {
$svc = Get-Service -Name "$CrcAdminHelperServiceName" -ErrorAction SilentlyContinue
if ($svc) {
Write-Host "Red Hat OpenShift Local Admin Helper service is already installed"
} else {
# New-Service cmdlet doesn't have a flag to set the arguments for admin-helper, it is passed using the -BinaryPathName itself
New-Service -Name 'crcAdminHelper' -DisplayName "Red Hat OpenShift Local Admin Helper" -Description "Perform administrative tasks for the user" -StartupType Automatic -BinaryPathName "$AdminHelperPath daemon" | Out-Null
}

Start-Service -Name 'crcAdminHelper' -Confirm:$false | Out-Null
}

function New-VsockGuestCommunicationRegistry {
# check if the registry path exists
$pathExists = Test-Path -Path $VsockGuestCommunicationRegistryPath -ErrorAction SilentlyContinue
# check if the property 'ElementName' exists at the path
$property = Get-ItemProperty -Path $VsockGuestCommunicationRegistryPath -Name 'ElementName' -ErrorAction SilentlyContinue
$propertyExists = $property.ElementName -eq "gvisor-tap-vsock"
if ($pathExists -and $propertyExists) {
Write-Host "Property ElementName in $VsockGuestCommunicationRegistryPath already exists"
} else {
# create the registry node
New-Item -Path $VsockGuestCommunicationRegistryPath -ErrorAction SilentlyContinue | Out-Null
# remove any pre-existing value
Remove-ItemProperty -Path $VsockGuestCommunicationRegistryPath -Name 'ElementName' -ErrorAction SilentlyContinue | Out-Null
# add the 'ElementName' property
New-ItemProperty -Path $VsockGuestCommunicationRegistryPath -Name 'ElementName' -Value 'gvisor-tap-vsock' -PropertyType string -Confirm:$false | Out-Null
}
}

0 comments on commit ad5d477

Please sign in to comment.