Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #103 from ingrammicro/release/mirage
Browse files Browse the repository at this point in the history
Release/mirage
  • Loading branch information
jrguerrero authored Aug 12, 2022
2 parents 8f911ac + eea1043 commit c88b288
Show file tree
Hide file tree
Showing 13 changed files with 493 additions and 234 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ _vendor*
coverage.txt
profile.out
.idea/*
.idea/**/*
.idea/**/*
dist/
29 changes: 29 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
builds:
- binary: cio
env:
- CGO_ENABLED=0
goos:
- windows
- darwin
- linux
goarch:
- amd64
- arm64
archives:
- format: binary
name_template: "{{ .ProjectName }}.{{ .Version }}.{{ .Arch }}.{{ .Os }}"
checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_checksums.txt'
release:
prerelease: auto
name_template: "{{.ProjectName}} {{.Tag}}"
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
16 changes: 0 additions & 16 deletions .goreleaser.yml

This file was deleted.

7 changes: 4 additions & 3 deletions api/types/bootstrapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
package types

type BootstrappingConfiguration struct {
Policyfiles []BootstrappingPolicyfile `json:"policyfiles,omitempty" header:"POLICY_FILES" show:"nolist"`
Attributes map[string]interface{} `json:"attributes,omitempty" header:"ATTRIBUTES" show:"nolist"`
AttributeRevisionID string `json:"attribute_revision_id,omitempty" header:"ATTRIBUTE_REVISION_ID"`
ConfigurationManagementSystem string `json:"cms,omitempty"`
Policyfiles []BootstrappingPolicyfile `json:"policyfiles,omitempty" header:"POLICY_FILES" show:"nolist"`
Attributes map[string]interface{} `json:"attributes,omitempty" header:"ATTRIBUTES" show:"nolist"`
AttributeRevisionID string `json:"attribute_revision_id,omitempty" header:"ATTRIBUTE_REVISION_ID"`
}

type BootstrappingPolicyfile struct {
Expand Down
49 changes: 46 additions & 3 deletions argo/argo-build-workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ spec:
templateRef:
name: workflow-template-sonar-template-1.1
template: go-dependency-track
when: "('{{workflow.parameters.branch}}' =~ '^v[0-9\\.]+$')"
when: "('{{workflow.parameters.branch}}' =~ '^v[0-9\\.]+$') || ('{{workflow.parameters.branch}}' == 'develop')"

- name: go-build
dependencies: [dependencies]
Expand All @@ -90,7 +90,7 @@ spec:
- name: test_path
value: .
- name: go-image
value: golang:1.16
value: golang:1.18.3
when: "{{workflow.parameters.skip_tests}} == false"

- name: sonar-branch-analysis
Expand Down Expand Up @@ -139,12 +139,20 @@ spec:
optional: true
when: "('{{workflow.parameters.pull_request}}' =~ '^[0-9]+$')"

- name: goreleaser
dependencies: [go-build]
template: goreleaser
arguments:
artifacts:
- name: src
from: "{{tasks.dependencies.outputs.artifacts.src}}"
when: "'{{workflow.parameters.branch}}' =~ '^v[0-9\\.]+'"

- name: go-build
inputs:
parameters:
- name: go-image
value: golang:1.16
value: golang:1.18.3
- name: notifiable_commits
- name: source_mountpath
value: /usr/src
Expand Down Expand Up @@ -223,3 +231,38 @@ spec:
cpu: 900m
limits:
cpu: 900m

- name: goreleaser
inputs:
parameters:
- name: go-image
value: golang:1.18.3
artifacts:
- name: src
path: "/src"
s3:
endpoint: local-minio-service:9000
bucket: argo-workflow
insecure: true
key: "{{workflow.uid}}/src.tgz"
accessKeySecret:
name: s3-credentials-generated-by-kustomize
key: S3_ACCESS_KEY_ID
secretKeySecret:
name: s3-credentials-generated-by-kustomize
key: S3_SECRET_ACCESS_KEY
script:
image: "{{inputs.parameters.go-image}}"
env:
- name: GITHUB_TOKEN
valueFrom:
secretKeyRef:
name: github-token
key: token
command: [sh]
source: |
cd /src
apk add --no-cache git
go install github.com/goreleaser/[email protected]
export GITHUB_TOKEN="${GITHUB_TOKEN}"
goreleaser
116 changes: 116 additions & 0 deletions bootstrapping/ansible.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package bootstrapping

import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/ingrammicro/cio/api/blueprint"
"github.com/ingrammicro/cio/api/types"
"github.com/ingrammicro/cio/utils/format"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

const (
inventoryFile = "inventory.yml"
variableFile = "variables.yml"
ansibleScript = "apply.sh"
)

// Subsidiary routine for commands processing
func applyAnsiblePolicyfiles(
ctx context.Context,
blueprintConfig *types.BootstrappingConfiguration,
bootstrappingSvc *blueprint.BootstrappingService,
bsProcess *bootstrappingProcess,
formatter format.Formatter,
) error {
err := prepareAnsibleInventory(ctx, bsProcess)
if err != nil {
formatter.PrintError("couldn't prepare inventory:", err)
return err
}
err = prepareAnsibleVariables(ctx, bsProcess)
if err != nil {
formatter.PrintError("couldn't prepare variables:", err)
return err
}
err = processAnsiblePolicyfiles(blueprintConfig, bootstrappingSvc, bsProcess)
if err != nil {
formatter.PrintError("couldn't process policyfiles:", err)
return err
}
return nil
}

func prepareAnsibleInventory(ctx context.Context, bsProcess *bootstrappingProcess) error {
log.Debug("prepareAnsibleInventory")
file, err := os.Create(inventoryFilePath(bsProcess.directoryPath))
if err != nil {
return fmt.Errorf("opening inventory file to write: %w", err)
}
defer file.Close()
inventory := map[string]interface{}{
"all": map[string]interface{}{
"hosts": map[string]interface{}{
"localhost": map[string]interface{}{
"ansible_connection": "local",
},
},
},
}
encoder := yaml.NewEncoder(file)
defer encoder.Close()
err = encoder.Encode(inventory)
if err != nil {
return fmt.Errorf("encoding inventory: %w", err)
}
return nil
}

func prepareAnsibleVariables(ctx context.Context, bsProcess *bootstrappingProcess) error {
log.Debug("prepareAnsibleVariables")
file, err := os.Create(variableFilePath(bsProcess.directoryPath))
if err != nil {
return fmt.Errorf("opening variable file to write: %w", err)
}
defer file.Close()
encoder := yaml.NewEncoder(file)
defer encoder.Close()
err = encoder.Encode(bsProcess.attributes.rawData)
if err != nil {
return fmt.Errorf("encoding variables: %w", err)
}
return nil
}

func inventoryFilePath(dir string) string {
return filepath.Join(dir, inventoryFile)
}

func variableFilePath(dir string) string {
return filepath.Join(dir, variableFile)
}

// processAnsiblePolicyfiles applies for each policy the required ansible-galaxy and ansible-playbook commands, reporting in bunches of N lines
func processAnsiblePolicyfiles(blueprintConfig *types.BootstrappingConfiguration, bootstrappingSvc *blueprint.BootstrappingService, bsProcess *bootstrappingProcess) error {
log.Debug("processAnsiblePolicyfiles")
for _, bsPolicyfile := range bsProcess.policyfiles {
policyfileDir := bsPolicyfile.Path(bsProcess.directoryPath)
command := fmt.Sprintf(
"cd %s && sh %s %s %s",
policyfileDir,
ansibleScript, inventoryFilePath(bsProcess.directoryPath), variableFilePath(bsProcess.directoryPath))
log.Debug(command)
bsProcess.cmsVersion = ""
// Custom method for chunks processing
fn := getBootstrapLogReporter(bootstrappingSvc, bsProcess, blueprintConfig)
if err := runCommand(fn, command, bsProcess.thresholdLines); err != nil {
return err
}
bsProcess.appliedPolicyfileRevisionIDs[bsPolicyfile.ID] = bsPolicyfile.RevisionID
}
return nil
}
Loading

0 comments on commit c88b288

Please sign in to comment.