Skip to content

Commit

Permalink
Merge pull request #1380 from lmm/automated-cherry-pick-of-#1373-upst…
Browse files Browse the repository at this point in the history
…ream-release-v3.20

[release-v3.20] Automated cherry pick of #1373: windows: move install-calico-windows.ps1 here
  • Loading branch information
marvin-tigera authored Dec 14, 2021
2 parents f0723f7 + 3467317 commit a6cb18b
Show file tree
Hide file tree
Showing 3 changed files with 600 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ NODE_CONTAINER_BIN_DIR=./dist/bin/
NODE_CONTAINER_BINARY = $(NODE_CONTAINER_BIN_DIR)/calico-node-$(ARCH)
WINDOWS_BINARY = $(NODE_CONTAINER_BIN_DIR)/calico-node.exe

WINDOWS_GEN_INSTALL_SCRIPT_BIN := hack/bin/gen-install-calico-windows-script

# Base URL of the Calico for Windows installation zip archive.
# This can be overridden for dev releases.
WINDOWS_ARCHIVE_BASE_URL ?= https://docs.projectcalico.org

# This is either "Calico" or "Calico Enterprise"
WINDOWS_INSTALL_SCRIPT_PRODUCT ?= Calico
WINDOWS_INSTALL_SCRIPT := dist/install-calico-windows.ps1

# Variables for the Windows packaging.
# Name of the Windows release ZIP archive.
WINDOWS_ARCHIVE_ROOT := windows-packaging/CalicoWindows
Expand Down Expand Up @@ -159,6 +169,8 @@ clean:
rm -f $(WINDOWS_ARCHIVE_ROOT)/libs/hns/hns.psm1
rm -f $(WINDOWS_ARCHIVE_ROOT)/libs/hns/License.txt
rm -f $(WINDOWS_ARCHIVE_ROOT)/cni/*.exe
rm -f $(WINDOWS_GEN_INSTALL_SCRIPT_BIN)
rm -f $(WINDOWS_INSTALL_SCRIPT)
rm -rf filesystem/included-source
rm -rf dist
rm -rf filesystem/etc/calico/confd/conf.d filesystem/etc/calico/confd/config filesystem/etc/calico/confd/templates
Expand Down Expand Up @@ -520,6 +532,9 @@ endif
$(MAKE) retag-build-images-with-registries RELEASE=true IMAGETAG=$(VERSION)
# Generate the `latest` images.
$(MAKE) retag-build-images-with-registries RELEASE=true IMAGETAG=latest
# Generate the install-calico-windows.ps1 script
$(MAKE) install-calico-windows-script
# Generate the Windows zip archives.
$(MAKE) release-windows-archive

## Produces the Windows ZIP archive for the release.
Expand Down Expand Up @@ -556,6 +571,11 @@ endif
-n $(VERSION) \
$(VERSION) $(WINDOWS_ARCHIVE)

# Update the release with the install-calico-windows.ps1 file too.
ghr -u projectcalico -r node \
-n $(VERSION) \
$(VERSION) $(WINDOWS_INSTALL_SCRIPT)

@echo "Finalize the GitHub release based on the pushed tag."
@echo ""
@echo " https://$(PACKAGE_NAME)/releases/tag/$(VERSION)"
Expand Down Expand Up @@ -642,6 +662,21 @@ build-windows-archive: $(WINDOWS_ARCHIVE_FILES) windows-packaging/nssm-$(WINDOWS
$(WINDOWS_ARCHIVE_BINARY): $(WINDOWS_BINARY)
cp $< $@

# Build the tool to generate the install-calico-windows.ps1 installation script.
$(WINDOWS_GEN_INSTALL_SCRIPT_BIN):
mkdir -p hack/bin
$(DOCKER_RUN) $(CALICO_BUILD) sh -c ' \
$(GIT_CONFIG_SSH) \
go build -o $@ ./hack/gen-install-calico-windows-script'

# Generate the install-calico-windows.ps1 installation script.
# For dev releases, override WINDOWS_ARCHIVE_BASE_URL.
install-calico-windows-script $(WINDOWS_INSTALL_SCRIPT): $(WINDOWS_GEN_INSTALL_SCRIPT_BIN)
$(WINDOWS_GEN_INSTALL_SCRIPT_BIN) \
-product "$(WINDOWS_INSTALL_SCRIPT_PRODUCT)" \
-version $(GIT_VERSION) \
-templatePath windows-packaging/install-calico-windows.ps1.tpl \
-baseUrl $(WINDOWS_ARCHIVE_BASE_URL) > $(WINDOWS_INSTALL_SCRIPT)

###############################################################################
# Utilities
Expand Down
126 changes: 126 additions & 0 deletions hack/gen-install-calico-windows-script/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (c) 2021 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"errors"
"flag"
"fmt"
"log"
"os"
"text/template"
)

type install struct {
Product string `json:"product"`
ProductName string `json:"productName"`
Version string `json:"version"`
BaseUrl string `json:"baseUrl"`
RootDir string `json:"rootDir"`
ZipFileName string `json:"zipFileName"`
}

var installs = map[string]install{
"Calico": {
Product: "Calico",
ProductName: "Calico for Windows",
RootDir: "CalicoWindows",
ZipFileName: "calico-windows.zip",
},
"Calico Enterprise": {
Product: "Calico Enterprise",
ProductName: "Tigera Calico for Windows",
RootDir: "TigeraCalico",
ZipFileName: "tigera-calico-windows.zip",
},
}

func newInstall(product, version, baseUrl string) (install, error) {
var data install

if install, ok := installs[product]; ok {
data = install
data.Version = version
data.BaseUrl = baseUrl
return data, nil
}
return data, fmt.Errorf("invalid product: %v", product)
}

var (
product string
version string
templatePath string
baseUrl string
)

// This program generates the Calico for Windows installation script for a given product, version and baseUrl.
//
// Examples:
//
// For Calico v3.21.1:
//
// gen-install-calico-windows-script \
// -product Calico \
// -version v3.21.1 \
// -templatePath windows-packaging/install-calico-windows.ps1.tpl \
// -baseUrl https://docs.projectcalico.org > install-calico-windows.ps1
//
//
// For Calico Enterprise v3.11.0:
//
// gen-install-calico-windows-script \
// -product "Calico Enterprise" \
// -version v3.11.0 \
// -templatePath windows-packaging/install-calico-windows.ps1.tpl \
// -baseUrl https://docs.tigera.io > install-calico-windows.ps1
func main() {
flag.StringVar(&product, "product", "", `product to generate install script for. either "Calico" or "Calico Enterprise"`)
flag.StringVar(&version, "version", "", `version`)
flag.StringVar(&templatePath, "templatePath", "", `path to the template for the installation script`)
flag.StringVar(&baseUrl, "baseUrl", "", `URL where the installation zip file will be hosted. Required for Calico only`)
flag.Parse()

log.Printf("product: %v, version: %v, templatePath: %v, baseUrl: %v", product, version, templatePath, baseUrl)

if product == "" || version == "" || templatePath == "" {
log.Fatalf("product, version, templatePath, and baseUrl must all be specified")
}

if product == "Calico" && baseUrl == "" {
log.Fatalf("baseUrl is required for Calico")
}

if _, err := os.Stat(templatePath); errors.Is(err, os.ErrNotExist) {
log.Fatalf("templatePath %v does not exist", templatePath)
}

data, err := newInstall(product, version, baseUrl)
if err != nil {
log.Fatalf("error generating installation script data: %v", err)
}

log.Printf("using install data: %+v", data)

t, err := template.ParseFiles(templatePath)
if err != nil {
log.Fatalf("error parsing template: %v", err)
}

err = t.Execute(os.Stdout, data)
if err != nil {
log.Fatalf("error rendering template: %v", err)
}
}
Loading

0 comments on commit a6cb18b

Please sign in to comment.