hub-clusters-creator 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e112445bd7b1d2f431a97502a2ca6fb52b0baaff096340aa3ab8fef52a0e6e37
4
+ data.tar.gz: e0d971cf87376aa8885c9b25a5c7b0645af4204487438f32c71d490e668ebd61
5
+ SHA512:
6
+ metadata.gz: 6ee5df2fa3bb45b1852ff7c206cd5ddb0dbe154f429f09bb26bd5c53a53f5438109cb72163620114de79a2721d1df4de8a550e175644173d8ad58ec60a931a85
7
+ data.tar.gz: fe053f1117639caaecb6e537d23d51db9f7fd015a8386ebb07fbea101ffc989ef91b227b34b767ae8a0c424dd8dd8435a25bace004b0b833aea567afa005a8f9
@@ -0,0 +1,5 @@
1
+ *.swp
2
+ *.gem
3
+ *.orig
4
+ account.json
5
+ dev.env
@@ -0,0 +1,28 @@
1
+ #
2
+ # vim:ts=2:sw=2:et
3
+ #
4
+ NAME=hub-bootstrap
5
+ AUTHOR ?= appvia
6
+ REGISTRY ?= quay.io
7
+ VERSION ?= latest
8
+
9
+ .PHONY: build test docker
10
+
11
+ default: build
12
+
13
+ build:
14
+ @echo "--> Building the GEM"
15
+ gem build hub-clusters-creator.gemspec
16
+
17
+ docker:
18
+ @echo "--> Building the docker image: ${REGISTRY}/${AUTHOR}/${NAME}:${VERSION}"
19
+ @(cd docker && docker build -t ${REGISTRY}/${AUTHOR}/${NAME}:${VERSION} .)
20
+
21
+ push:
22
+ @echo "--> Pushing the image to respository"
23
+ docker push ${REGISTRY}/${AUTHOR}/${NAME}:${VERSION}
24
+
25
+ clean:
26
+ @echo "--> Performing a cleanup"
27
+ @docker rmi -f ${REGISTRY}/${AUTHOR}/${NAME}:${VERSION} 2>/dev/null
28
+ @rm -f *.gem
@@ -0,0 +1,42 @@
1
+ #!/bin/ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Copyright (C) 2019 Rohith Jayawardene <gambol99@gmail.com>
5
+ #
6
+ # This program is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU General Public License
8
+ # as published by the Free Software Foundation; either version 2
9
+ # of the License, or (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '.', '../lib')
20
+
21
+ require 'hub-clusters-creator'
22
+
23
+ creator = HubClustersCreator.new(
24
+ client_id: ENV['AKS_CLIENT_ID'],
25
+ client_secret: ENV['AKS_CLIENT_SECRET'],
26
+ provider: 'aks',
27
+ region: 'uksouth',
28
+ subscription: ENV['AKS_SUBSCRIPTION'],
29
+ tenant: ENV['AKS_TENANT']
30
+ )
31
+ puts creator.provision(
32
+ description: 'just a test',
33
+ domain: 'akslearning.appvia.io',
34
+ github_client_id: ENV['GITHUB_CLIENT_ID'],
35
+ github_client_secret: ENV['GITHUB_CLIENT_SECRET'],
36
+ github_organization: ENV['GITHUB_ORG'],
37
+ grafana_hostname: 'grafana.akslearning.appvia.io',
38
+ size: 3,
39
+ ssh_key: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB/RZqb2UuuXmJbwml/G1InXVwgNe1tgz+Xd6HTfPlS00GSHTynjyLNKWbDBZhzj2jsM/40TyCZLLKai8hV4Lc72ijMsEAbSJ8gou0Kq6P25GxY7uQcCeACPT6AbQCZjiyzPCFPy+QC56W64QOtS+SLRefY8g4uGAz01ZfhChK0J2Mev1oiPXRAlOmzGz9fPpx4J8YE0R/OkEoWQaWdKVOV6nq2nz3vbwvLfbdK0EK/KAivwv9mlgalwSr3bgAXwRS2nXq7vyITvpjgDYcRu85fWSE9yeMyw4S10ya5/ALi+p4HkSbDaz2YGwksSup1lBeRflSKKghWba4+dzf4iO87cwYCN8erVxATznuPoB0TWYYOXrUwc3yGadOV5GkzQLLDhIzJ8BmMb/iT5jHBdHs1bK1lTThwPllmgkRANFIWMC9jyA3BgJp2vtHcLyOXOsFFaXqpjXZ2tvjaBVjaaDUY+CE2rbJMBquUMbvtxwBtkhVVQ4COofUsXhKOcwjOyo1Hw1hcJ9ig6DiuzYhiPr/JZPUk2l8ovAKkGwFjEui7jltcWzGrmRzFp8+Xdq9wl/BEeglPv+eZuuwXgAWRge992kepda1f5OlBUl8JrohbxtVcv+zvmVaiZqcrKpyNbtN0ELMA8e4K6wCllQ0oBr17oTG7JwMVgfvTCgsI+U/ jest@starfury',
40
+ name: 'dev',
41
+ version: '1.14.3'
42
+ )
@@ -0,0 +1,48 @@
1
+ #!/bin/ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Copyright (C) 2019 Rohith Jayawardene <gambol99@gmail.com>
5
+ #
6
+ # This program is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU General Public License
8
+ # as published by the Free Software Foundation; either version 2
9
+ # of the License, or (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '.', '../lib')
20
+
21
+ require 'hub-clusters-creator'
22
+ require 'pp'
23
+
24
+ account = File.read('account.json')
25
+ region = 'europe-west2'
26
+ project = 'gke-learning-242311'
27
+
28
+ #creator = HubClustersCreator.new(
29
+ # account: account,
30
+ # provider: 'gke',
31
+ # project: project,
32
+ # region: region
33
+ #)
34
+ #puts creator.provision(
35
+ # description: 'just a test',
36
+ # domain: 'gkelearning.appvia.io',
37
+ # github_client_id: ENV['GITHUB_CLIENT_ID'],
38
+ # github_client_secret: ENV['GITHUB_CLIENT_SECRET'],
39
+ # github_organization: ENV['GITHUB_ORG'],
40
+ # grafana_hostname: 'grafana.gkelearning.appvia.io',
41
+ # grafana_ingress: true,
42
+ # logging: true,
43
+ # machine_type: 'n1-standard-1',
44
+ # name: 'test',
45
+ # version: '1.13.7-gke.8'
46
+ #)
47
+
48
+ puts HubClustersCreator.schema.to_json
@@ -0,0 +1,20 @@
1
+ FROM fedora:30
2
+ MAINTAINER Rohith Jayawardene <gambol99@gmail.com>
3
+
4
+ ENV ARCH="amd64" \
5
+ HELM_VERSION="2.14.1" \
6
+ KUBECTL_VERSION="1.14.0"
7
+
8
+ RUN dnf install -y bash curl jq
9
+
10
+ RUN curl -sL https://storage.googleapis.com/kubernetes-release/release/v${KUBECTL_VERSION}/bin/linux/${ARCH}/kubectl -o /usr/bin/kubectl && \
11
+ curl -sL https://get.helm.sh/helm-v${HELM_VERSION}-linux-${ARCH}.tar.gz | tar -C /tmp -xzf - && mv /tmp/linux-${ARCH}/helm /usr/bin/helm && \
12
+ chmod +x /usr/bin/kubectl /usr/bin/helm && \
13
+ rm -rf /tmp/linux-${ARCH}
14
+
15
+ RUN kubectl version --client && \
16
+ helm version --client
17
+
18
+ COPY entrypoint.sh /entrypoint.sh
19
+
20
+ ENTRYPOINT [ "/entrypoint.sh" ]
@@ -0,0 +1,89 @@
1
+ #!/bin/bash
2
+ #
3
+ # Copyright (C) 2019 Rohith Jayawardene <gambol99@gmail.com>
4
+ #
5
+ # This program is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU General Public License
7
+ # as published by the Free Software Foundation; either version 2
8
+ # of the License, or (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ #
18
+
19
+ log() { (2>/dev/null echo -e "$@"); }
20
+ info() { log "[$(date)][info] $@"; }
21
+ failed() { log "[$(date)][fail] $@"; }
22
+ error() { log "[$(date)][error] $@"; }
23
+
24
+ CONFIG_DIR="${CONFIG_DIR:-"/config"}"
25
+ HELM_DIR="${CONFIG_DIR}/bundles"
26
+ HELM_REPOS="${HELM_DIR}/repositories"
27
+ HELM_BUNDLES="${HELM_DIR}/charts"
28
+ KUBE_DIR="${CONFIG_DIR}/manifests"
29
+
30
+ # deploy-manifests deploys all the files in the manifests directory
31
+ deploy-manifests() {
32
+ info "deploying the kubernetes manifests from: ${KUBE_DIR}"
33
+ [[ -d "${KUBE_DIR}" ]] || return 0
34
+
35
+ ret=0
36
+ for filename in ${KUBE_DIR}/*; do
37
+ [[ -f "${filename}" ]] || continue
38
+ [[ ${filename} =~ ^.*\.ya?ml ]] || continue
39
+
40
+ info "deploying the manifest: ${filename}"
41
+ if ! kubectl apply -f ${manifest}; then
42
+ error "failed to deploy the manifest: ${filename}"
43
+ fi
44
+ done
45
+ return $ret
46
+ }
47
+
48
+ # deploy-bundles is responsible for deploying charts into the cluster
49
+ # loki,bundles/loki,overrides/loki.yaml
50
+ deploy-bundles() {
51
+ info "installing helm tiller service"
52
+ helm init --wait --service-account=sysadmin >/dev/null || return 1
53
+
54
+ if [[ -f ${HELM_REPOS} ]]; then
55
+ info "installing any repository requirements"
56
+ while IFS=',' read name repository; do
57
+ info "adding the helm repository: ${repository}"
58
+ helm repo add ${name} ${repository} || return 1
59
+ done < <(cat ${HELM_REPOS})
60
+
61
+ info "updating the repositories cache"
62
+ helm repo update || return 1
63
+ fi
64
+
65
+ if [[ -f ${HELM_BUNDLES} ]]; then
66
+ info "installing the helm charts"
67
+ while IFS=',' read chart namespace options; do
68
+ namespace=${namespace:-"default"}
69
+ name=${chart%%/*}
70
+ if helm ls -q | grep ^${name}; then
71
+ info "installing chart: ${chart}, namespace: ${namespace}, options: ${options}"
72
+ helm upgrade --wait ${chart} --namespace ${namespace} ${options} || return 1
73
+ else
74
+ info "upgrading chart: ${chart}, namespace: ${namespace}, options: ${options}"
75
+ helm install --wait ${chart} --namespace ${namespace} ${options} || return 1
76
+ fi
77
+ done < <(cat ${HELM_BUNDLES})
78
+ fi
79
+ }
80
+
81
+ deploy-manifests || {
82
+ error "failed to deploy the kubernetes manifests";
83
+ exit 1;
84
+ }
85
+ deploy-bundles || {
86
+ error "failed to deploy the software manifests";
87
+ exit 1;
88
+ }
89
+
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/ruby
2
+ # frozen_string_literal: true
3
+
4
+ # rubocop:disable Metrics/LineLength
5
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '.', 'lib/hub-clusters-creator')
6
+
7
+ require 'version'
8
+
9
+ Gem::Specification.new do |s|
10
+ s.name = 'hub-clusters-creator'
11
+ s.version = HubClustersCreator::VERSION
12
+ s.platform = Gem::Platform::RUBY
13
+ s.date = '2019-08-02'
14
+ s.authors = ['Rohith Jayawardene']
15
+ s.email = 'gambol99@gmail.com'
16
+ s.homepage = 'http://rubygems.org/gems/hub-clusters-creator'
17
+ s.summary = 'An agent used to provision GKE clusters for the Appvia Hub'
18
+ s.description = 'An agent used to provision GKE clusters for the Appvia Hub '
19
+ s.license = 'GPL-2.0'
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
23
+
24
+ s.add_dependency('azure_mgmt_container_service', '~> 0.18.5')
25
+ s.add_dependency('azure_mgmt_dns', '~> 0.18.5')
26
+ s.add_dependency('azure_mgmt_resources', '~> 0.18.5')
27
+ s.add_dependency('deep_merge', '~> 1.2.1')
28
+ s.add_dependency('google-api-client', '~> 0.30')
29
+ s.add_dependency('googleauth', '~> 0.7')
30
+ s.add_dependency('json_schema', '~> 0.20.4')
31
+ s.add_dependency('k8s-client', '~> 0.10')
32
+ s.add_dependency('stringio', '~> 0.0.2')
33
+ end
34
+ # rubocop:enable Metrics/LineLength
@@ -0,0 +1,49 @@
1
+ # rubocop:disable Naming/FileName
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # rubocop:enable Naming/FileName
6
+ # Copyright (C) 2019 Rohith Jayawardene <gambol99@gmail.com>
7
+ #
8
+ # This program is free software; you can redistribute it and/or
9
+ # modify it under the terms of the GNU General Public License
10
+ # as published by the Free Software Foundation; either version 2
11
+ # of the License, or (at your option) any later version.
12
+ #
13
+ # This program is distributed in the hope that it will be useful,
14
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ # GNU General Public License for more details.
17
+ #
18
+ # You should have received a copy of the GNU General Public License
19
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
+
21
+ require_relative 'hub-clusters-creator/agent'
22
+ require_relative 'hub-clusters-creator/version'
23
+
24
+ # Clusters providers the wrapper to the providers
25
+ module HubClustersCreator
26
+ def self.version
27
+ HubClustersCreator::VERSION
28
+ end
29
+
30
+ def self.new(name)
31
+ HubClustersCreator::Agent.new(name)
32
+ end
33
+
34
+ def self.defaults(name)
35
+ HubClustersCreator::Agent.defaults(name)
36
+ end
37
+
38
+ def self.schema
39
+ o = []
40
+ HubClustersCreator::Agent.providers.each do |x|
41
+ o.push(
42
+ id: x,
43
+ init_options: HubClustersCreator::Agent.provider_schema(x),
44
+ provision_options: HubClustersCreator::Agent.cluster_schema(x)
45
+ )
46
+ end
47
+ o
48
+ end
49
+ end
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (C) 2019 Rohith Jayawardene <gambol99@gmail.com>
4
+ #
5
+ # This program is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU General Public License
7
+ # as published by the Free Software Foundation; either version 2
8
+ # of the License, or (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+ #
18
+ require 'deep_merge'
19
+ require 'json'
20
+ require 'json_schema'
21
+
22
+ require 'hub-clusters-creator/errors'
23
+ require 'hub-clusters-creator/logging'
24
+ require 'hub-clusters-creator/providers/aks/azure.rb'
25
+ require 'hub-clusters-creator/providers/gke/gke.rb'
26
+
27
+ # rubocop:disable Metrics/MethodLength,Metrics/LineLength
28
+ module HubClustersCreator
29
+ # Agent is the main agent class
30
+ class Agent
31
+ include Errors
32
+ include Logging
33
+
34
+ # rubocop:disable Metrics/AbcSize
35
+ def initialize(provider)
36
+ @provider_name = provider[:provider]
37
+
38
+ # @step: validate the provider configuration
39
+ JsonSchema.parse!(HubClustersCreator::Agent.provider_schema(@provider_name)).validate(provider)
40
+
41
+ # @step: create and return a provider instance
42
+ case @provider_name
43
+ when 'gke'
44
+ @provider = HubClustersCreator::Providers::GKE.new(
45
+ account: provider[:account],
46
+ project: provider[:project],
47
+ region: provider[:region]
48
+ )
49
+ when 'aks'
50
+ @provider = HubClustersCreator::Providers::AKS.new(
51
+ client_id: provider[:client_id],
52
+ client_secret: provider[:client_secret],
53
+ region: provider[:region],
54
+ subscription: provider[:subscription],
55
+ tenant: provider[:tenant]
56
+ )
57
+ else
58
+ raise ArgumentError, "cloud provider: #{@provider_name} not supported"
59
+ end
60
+ end
61
+ # rubocop:enable Metrics/AbcSize
62
+
63
+ # providers provides a list of providers
64
+ def self.providers
65
+ %w[aks gke]
66
+ end
67
+
68
+ # defaults builds the default from the schema
69
+ def self.defaults(name)
70
+ values = {}
71
+ cluster_schema(name)['properties'].reject { |x, _v| x == 'authorized_master_cidrs' }.each do |k, v|
72
+ values[k.to_sym] = v['default']
73
+ end
74
+ # @TODO find a better way of doing this
75
+ unless values[:authorized_master_cidrs]
76
+ values[:authorized_master_cidrs] = [{ name: 'any', cidr: '0.0.0.0/0' }]
77
+ end
78
+ values
79
+ end
80
+
81
+ # provider_schema returns the provider schema
82
+ def self.provider_schema(name)
83
+ schemas(name).first
84
+ end
85
+
86
+ # cluster_schema returns a cluster schema for a specific provider
87
+ def self.cluster_schema(name)
88
+ schemas(name).last
89
+ end
90
+
91
+ # schemas returns the json schemais defining the providers configuration schema and the
92
+ # cluster schema for tha cloud provider
93
+ def self.schemas(name)
94
+ file = "#{__dir__}/providers/#{name}/schema.yaml"
95
+ raise ArgumentError, "provider: '#{name}' is not supported" unless File.exist?(file)
96
+
97
+ # loads and parses both the provider and cluster schema
98
+ provider_schemas = YAML.load_stream(File.read(file))
99
+ # load and parse the base schema which is used across all providers
100
+ provider_base = YAML.safe_load(File.read("#{__dir__}/providers/schema.yaml"))
101
+ # we deep merge the provider with the defaults
102
+ provider_schemas.last.deep_merge(provider_base)
103
+
104
+ provider_schemas
105
+ end
106
+
107
+ # destroy is responsible is tearing down the cluster
108
+ def destroy(name, options)
109
+ @provider.destroy(name, options)
110
+ end
111
+
112
+ # provision is responsible for provisioning the cluster
113
+ # rubocop:disable Lint/RescueException, Metrics/AbcSize
114
+ def provision(options)
115
+ name = options[:name]
116
+ config = HubClustersCreator.defaults(@provider_name).merge(options)
117
+
118
+ # @step: provision the cluster if not already there
119
+ begin
120
+ schema = HubClustersCreator::Agent.provider_schema(@provider_name)
121
+ # verify the options
122
+ JsonSchema.parse!(schema).validate(config)
123
+ # provision the cluster
124
+ @provider.create(name, config)
125
+ rescue InfrastructureError => e
126
+ error "failed to provision the infrastructure: #{name}, error: #{e}"
127
+ raise e
128
+ rescue ConfigurationError => e
129
+ error "invalid configuration for cluster: #{name}, error: #{e}"
130
+ raise e
131
+ rescue InitializerError => e
132
+ error "failed to initialize cluster: #{name}, error: #{e}"
133
+ raise e
134
+ rescue Exception => e
135
+ error "failed to provision the cluster: #{name}, error: #{e}"
136
+ raise e
137
+ end
138
+ end
139
+ # rubocop:enable Lint/RescueException, Metrics/AbcSize
140
+ end
141
+ end
142
+ # rubocop:enable Metrics/MethodLength,Metrics/LineLength