sct 1.0.8 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77a9f9b0da2f262d9c336ee3f6f912bb0e097f0caf9d91adbe1b3dd9ad33059d
4
- data.tar.gz: 58cbc6da8d20cb35fdc6f1d12d326472ff0a40466010f7b9e4130c7f201a52ec
3
+ metadata.gz: 87ea56635397f45831d78759ad65fa074bbde63827fc8c8fba75903f71bf45a6
4
+ data.tar.gz: 0c72ba5441f383c3dea4f5912c33734822774c93054fdd332a7f57042d46ec66
5
5
  SHA512:
6
- metadata.gz: d8379c969766240db807fb92dea072eea56d7e3e0f0148ee2e9c4318e32eb60903cc5bd9a20353ac5bb031582795e7f20661cfe7a7829151dd6d1d5a12264d57
7
- data.tar.gz: 30eded070cff1db7d533c2d3f36e850204ff78c7be2696d504e0d4e1eb86f8564da26278f04621b021f3cfb2f29805f4ece5d9c6a3b156a7f6c145118acfb74e
6
+ metadata.gz: 508892aaef3fffe214203682a28f8976abb681c01243154df5c4ef061a3ebd84c97a510858ae91ead399ab56e68329db6b318ff434afa9b48ca966aad62d89cb
7
+ data.tar.gz: 6b7868aaf906825de525197e174766edca7de50e90775cd9a130a48c6a4c2cd305250e455c964da5088cead2799250d4083a973101a1288bfd5ab1a093c80b5c
@@ -0,0 +1,136 @@
1
+ require 'commander'
2
+ require_relative 'runner'
3
+ require 'pp'
4
+
5
+ module Henk
6
+ class CommandsGenerator
7
+ include Commander::Methods
8
+
9
+ def self.start
10
+ self.new.run
11
+ end
12
+
13
+ def run
14
+ add_commander_methods
15
+
16
+ Henk::Runner.new.verify_cluster_active
17
+
18
+ program :name, 'henk'
19
+ program :version, Sct::VERSION
20
+ program :description, 'CLI for \'henk\' - Interact with Henk 3.0 to be able to create/copy/delete customers'
21
+
22
+ $dev = false
23
+ $verbose = false
24
+ global_option('--dev') { $dev = true }
25
+ global_option('--verbose') { $verbose = true }
26
+
27
+ command :create do |c|
28
+ c.syntax = 'sct henk create'
29
+ c.description = 'Create a client in your local development.'
30
+ c.required_option '--domain DOMAIN', 'The domain name of the new customer.'
31
+ c.required_option '--organization-name ORGANIZATION_NAME', 'The name of the organization.'
32
+ c.required_option '--modules MODULES', 'Allowed modules. ex: 01, 02, 03, ...'
33
+
34
+ c.action do |args, options|
35
+ c.validate options
36
+
37
+ options.dev = $dev
38
+ options.verbose = $verbose
39
+ Henk::Runner.new.henk "create", options
40
+ Henk::Runner.new.encrypt_config_data options.domain
41
+ end
42
+ end
43
+
44
+ command :copy do |c|
45
+ c.syntax = 'sct henk copy'
46
+ c.description = 'Copy a client from another environment to your local environment'
47
+ c.required_option '--domain DOMAIN', 'The domain to copy.'
48
+ c.option '--source-environment SOURCE_ENVIRONMENT', 'The name of the source environment (currently only local allowed).'
49
+ c.option '--target-domain DOMAIN', 'The name of the domain on the local environment. When making a local copy it cannot be the same name'
50
+ c.option '--overwrite', 'Set this flag if you want to overwrite an existing target-domain.'
51
+ c.option '--copy-data', 'Set this flag if you want to copy the data of the source client'
52
+
53
+ # Specify command options here
54
+ c.action do |args, options|
55
+ c.validate options
56
+
57
+ unless options.source_environment
58
+ options.source_environment = "local"
59
+ end
60
+
61
+ # Temporary provision until we can copy environments from Accept
62
+ if options.source_environment != "local"
63
+ raise ArgumentError.new("It's currently not possible to copy an cloud environment.".yellow)
64
+ end
65
+
66
+ options.target_environment = "local"
67
+
68
+ unless options.target_domain
69
+ options.target_domain = options.domain
70
+ end
71
+
72
+ if options.source_environment == options.target_environment \
73
+ and options.domain == options.target_domain
74
+ raise ArgumentError.new("The source and target domains cannot be the same.")
75
+ end
76
+
77
+ options.dev = $dev
78
+ options.verbose = $verbose
79
+ Henk::Runner.new.henk "copy", options
80
+ Henk::Runner.new.encrypt_config_data options.target_domain
81
+ Henk::Runner.new.synchronize_employees options.target_domain
82
+ end
83
+ end
84
+
85
+ command :remove do |c|
86
+ c.syntax = 'sct henk remove'
87
+ c.description = 'Remove a client from your local environment.'
88
+ c.required_option '--domain DOMAIN', 'The domain to remove.'
89
+ c.option '--user-email EMAIL', 'The email address of the user removing the customer'
90
+
91
+ c.action do |args, options|
92
+ c.validate options
93
+ options.dev = $dev
94
+ options.verbose = $verbose
95
+ Henk::Runner.new.henk 'remove', options
96
+ end
97
+ end
98
+
99
+ command :"list-clients" do |c|
100
+ c.syntax = 'sct henk list-clients'
101
+ c.description = 'List all clients that are available on the dev environment'
102
+
103
+ c.action do |args, options|
104
+ Henk::Runner.new.list_clients
105
+ end
106
+ end
107
+
108
+ run!
109
+ end
110
+
111
+ # Ruben's suggestion but still need some workaround
112
+ def add_commander_methods
113
+ Commander::Command.class_eval {
114
+ attr_accessor :required
115
+
116
+ def required_option(*arg, &block)
117
+ unless self.required
118
+ @required = []
119
+ end
120
+
121
+ option = arg.select{|item| item.start_with?('--')}.map{|a| a.gsub(/\-\-([^\s]*)(.*)/, '\1')}.shift
122
+ self.required << option.to_s.tr('-', '_')
123
+ self.option(*arg)
124
+ end
125
+
126
+ def validate options
127
+ self.required.each do |option|
128
+ if options.__hash__[:"#{option}"].nil? || options.__hash__[:"#{option}"] == ''
129
+ raise ArgumentError.new("The --#{option} argument is a required argument")
130
+ end
131
+ end
132
+ end
133
+ }
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,7 @@
1
+ module Henk
2
+
3
+ # import the helper functionality from SCT
4
+ Helpers = Sct::Helper
5
+ UI = Sct::UI
6
+
7
+ end
@@ -0,0 +1,175 @@
1
+ require 'pp'
2
+
3
+ module Henk
4
+ class Runner
5
+ attr_accessor :options_mapping
6
+
7
+ def initialize
8
+ @options_mapping = {
9
+ "organization_name" => {
10
+ "name" => "organization-name"
11
+ },
12
+ "modules" => {
13
+ "name" => "modules",
14
+ "type" => "array"
15
+ },
16
+ "database_source" => {
17
+ "name" => "database-source"
18
+ },
19
+ "target_domain" => {
20
+ "name" => "target-domain"
21
+ },
22
+ "copy_data" => {
23
+ "name" => "copy-data"
24
+ },
25
+ "source_environment" => {
26
+ "name" => "source-environment"
27
+ },
28
+ "target_environment" => {
29
+ "name" => "target-environment"
30
+ },
31
+ "user_email" => {
32
+ "name" => "user-email"
33
+ }
34
+ }
35
+ end
36
+
37
+ def run command
38
+ if !system command
39
+ raise command.red
40
+ end
41
+
42
+ rescue Interrupt
43
+ end
44
+
45
+ def verify_cluster_active
46
+ required_containers = [
47
+ "proactive-config",
48
+ "proactive-frame",
49
+ "mysql-service"
50
+ ]
51
+
52
+ if !system "docker network inspect spend-cloud > /dev/null"
53
+ puts "Error: ".red + "The local spend-cloud cluster doesn't seem to be running."
54
+ exit(1)
55
+ end
56
+
57
+ connected_to_network = `docker network inspect spend-cloud | jq '.[0] | .Containers | map(select((.Name | test("^#{required_containers.join('|^')}")))) | length'`
58
+
59
+ if connected_to_network.to_s.strip! != "3"
60
+ puts "Error: ".red + "Not all required services are running or connected to the cluster (#{connected_to_network}/3)"
61
+ exit(1)
62
+ end
63
+
64
+ return true
65
+ end
66
+
67
+ def henk command, options = {}
68
+ begin
69
+ volumes = get_volumes_string(options.dev)
70
+ image = get_latest_image
71
+ verbose = options.verbose
72
+
73
+ options = options_to_string options
74
+ to_run = "docker run -it --rm --network spend-cloud #{volumes} #{image} #{command} #{options}"
75
+
76
+ if verbose
77
+ puts "Running: #{to_run.green}"
78
+ end
79
+
80
+ self.run to_run
81
+ end
82
+ end
83
+
84
+ def list_clients
85
+ self.run "docker exec -it mysql-service mysql -u root -sN -e 'SELECT `043` FROM `spend-cloud-config`.`00_settings` ORDER BY `043` ASC'"
86
+ end
87
+
88
+ def encrypt_config_data domain
89
+ puts "Manually encrypting config data for #{domain}".green
90
+ php_command = "php /var/www/scripts/encryptConfigData.php -c='#{domain}' --execute"
91
+ command = 'docker exec -it proactive-config bash -c "#{php_command}"'
92
+ self.run command
93
+ end
94
+
95
+ def synchronize_employees domain
96
+ puts "Manually synchronizing employees with shared storage for #{domain}".green
97
+ php_command = "php /var/www/scripts/syncEmployees.php --client='#{domain}'"
98
+ command = 'docker exec -it proactive-frame bash -c "#{php_command}"'
99
+ self.run command
100
+ end
101
+
102
+ def get_latest_image
103
+ latest_version = `gcloud container images list-tags eu.gcr.io/dev-pasc-vcdm/henk-app --limit 1 --format=\"json\" | jq -r '[.[0].tags | sort_by(length)[] | select(match(\"^[0-9]+$\"))] | first'`
104
+ return "eu.gcr.io/dev-pasc-vcdm/henk-app:#{latest_version.strip}"
105
+ end
106
+
107
+ def get_volumes_string dev = false
108
+ volumes = {
109
+ "spend-cloud_shared-storage" => "/data"
110
+ }
111
+
112
+ if dev
113
+ repoPath = Sct::Helper.homePath + "/development/henk"
114
+
115
+ if not Dir.exists?(repoPath)
116
+ puts "Warning: ".yellow + "To run in development mode you need a checkout for the Henk repository. Continuing in normal mode."
117
+ else
118
+ dev_volumes = {
119
+ repoPath + "/modules" => "/var/scripts/modules",
120
+ repoPath + "/client.py" => "/var/scripts/client.py",
121
+ repoPath + "/environment_mapping.json" => "/var/scripts/environment_mapping.json",
122
+ repoPath + "/logging-config.json" => "/var/scripts/logging-config.json",
123
+ repoPath + "/schemas" => "/var/scripts/schemas",
124
+ repoPath + "/resources" => "/var/local"
125
+ }
126
+
127
+ volumes = volumes.merge(dev_volumes)
128
+ end
129
+ end
130
+
131
+ return volumes.map{|k,v| "-v #{k}:#{v}"}.join(' ')
132
+ end
133
+
134
+ def options_to_string options = {}
135
+ options_str = ''
136
+
137
+ if options.verbose
138
+ options_str += '--verbose '
139
+ end
140
+
141
+ if options.overwrite
142
+ options_str += '--overwrite '
143
+ end
144
+
145
+ if options.copy_data
146
+ options_str += '--copy-data '
147
+ end
148
+
149
+ options = options.__hash__
150
+
151
+ options.delete(:dev)
152
+ options.delete(:verbose)
153
+ options.delete(:overwrite)
154
+ options.delete(:copy_data)
155
+
156
+ parsed_options = []
157
+
158
+ options.each do |k,v|
159
+ if self.options_mapping["#{k}"]
160
+ mapping = self.options_mapping["#{k}"]
161
+ k = mapping['name']
162
+
163
+ if mapping['type'] == "array"
164
+ v = v.split(',').join(' ')
165
+ end
166
+ end
167
+
168
+ parsed_options << "--#{k} #{v}"
169
+ end
170
+
171
+ options_str += parsed_options.join(' ')
172
+ return options_str
173
+ end
174
+ end
175
+ end
data/henk/lib/henk.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'sct_core'
2
+ require 'terminal-table'
3
+
4
+ require_relative 'henk/runner'
5
+ require_relative 'henk/module'
@@ -6,7 +6,10 @@ module Sct
6
6
  end
7
7
 
8
8
  def execute args, options
9
- branch = ENV['CI_BRANCH'] || 'master'
9
+ branch = ENV['CI_BRANCH']
10
+ if not branch or branch == ""
11
+ branch = 'master'
12
+ end
10
13
 
11
14
  # We need to set this locally, so that we don't save this at the repository
12
15
  pipeline_endpoint = ENV['CI_MERGE_REQUEST_PIPELINE']
@@ -20,14 +23,27 @@ module Sct
20
23
  error "Private token not set"
21
24
  end
22
25
 
23
- if options.proactive_frame
24
- UI.important("Requesting to pull database for proactive frame")
25
- process_request = `curl -X POST -F token=#{private_token} -F ref=#{branch} -F variables[DB_PULL_FRAME]=true #{pipeline_endpoint}`
26
- end
27
-
28
- if options.proactive_config
29
- UI.important("Requesting to pull database for proactive config")
30
- process_request = `curl -X POST -F token=#{private_token} -F ref=#{branch} -F variables[DB_PULL_CONFIG]=true #{pipeline_endpoint}`
26
+ if options.all
27
+ UI.important("Requesting to pull database changes for proactive frame and proactive config")
28
+ process_request = `curl -X POST -F token=#{private_token} -F ref=master -F variables[DB_PULL_FRAME]=true -F variables[DB_PULL_CONFIG]=true -F variables[REMOTE_BRANCH_NAME]=#{branch} #{pipeline_endpoint}`
29
+ else
30
+ db_pull_frame = ""
31
+ if options.proactive_frame
32
+ UI.important("Requesting to pull database for proactive frame")
33
+ db_pull_frame = "-F variables[DB_PULL_FRAME]=true"
34
+ end
35
+
36
+ db_pull_config = ""
37
+ if options.proactive_config
38
+ UI.important("Requesting to pull database for proactive config")
39
+ db_pull_config = "-F variables[DB_PULL_CONFIG]=true"
40
+ end
41
+
42
+ if db_pull_frame != "" or db_pull_config != ""
43
+ process_request = `curl -X POST -F token=#{private_token} -F ref=master #{db_pull_frame} #{db_pull_config} -F variables[REMOTE_BRANCH_NAME]=#{branch} #{pipeline_endpoint}`
44
+ else
45
+ error "Missing options. Use --help to see the available options"
46
+ end
31
47
  end
32
48
  end
33
49
  end
@@ -29,15 +29,6 @@ module Sct
29
29
  def execute args, options
30
30
  services = manifest["services"].to_a
31
31
 
32
- if services.length != 1
33
- error "Currently sct only supports a single service declaration in '#{@@file}'. Contact the infra guild if you consider this a limitation."
34
- end
35
-
36
- service, service_spec = services.first
37
-
38
- container = service_spec["container_name"]
39
- command = service_spec["command"] || ""
40
-
41
32
  if Sct::Helper.is_windows?
42
33
  host_machine_IP = (options.wsl_debugger ? `hostname -I | awk '{print $1}'` : `powershell.exe -c '(Test-Connection -ComputerName $env:computername -count 1).ipv4address.IPAddressToString' | dos2unix`).chomp
43
34
  elsif Sct::Helper.is_mac_os?
@@ -66,11 +57,20 @@ module Sct
66
57
  system "docker image prune -f"
67
58
  end
68
59
 
69
- return unless dc "rm --stop --force #{service}"
60
+ for service, settings in services
61
+ return unless dc "rm --stop --force #{service}"
62
+ end
63
+
64
+ begin
65
+ dc_dev "up", env
66
+ rescue SystemExit, Interrupt
67
+ end
70
68
 
71
- dc_dev "run --rm --service-ports --name #{container} #{service} #{command}", env
69
+ dc_dev "rm --force --stop"
72
70
 
73
- dc "up --detach #{service}"
71
+ for service, settings in services
72
+ dc "up --detach #{service}"
73
+ end
74
74
  end
75
75
 
76
76
  end
@@ -21,10 +21,6 @@ module Sct
21
21
 
22
22
  services = manifest["services"].to_a
23
23
 
24
- if services.length != 1
25
- error "Currently sct only supports a single service declaration in '#{file}'. Contact the infra guild if you consider this a limitation."
26
- end
27
-
28
24
  service, service_spec = services.first
29
25
 
30
26
  container = service_spec["container_name"]
@@ -57,6 +57,7 @@ module Sct
57
57
  command :'database-pull' do |c|
58
58
  c.syntax = 'sct database pull'
59
59
  c.description = 'pull database changes for proactive frame and proactive config'
60
+ c.option '--all', 'pull the database changes for all repositories'
60
61
  c.option '--proactive-frame', 'pull the database changes from proactive-frame'
61
62
  c.option '--proactive-config', 'pull the database changes from proactive-config'
62
63
 
data/sct/lib/sct/tools.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  module Sct
2
2
  TOOLS = [
3
3
  :sct,
4
- :cluster
4
+ :cluster,
5
+ :henk
5
6
  ]
6
7
 
7
8
  # a list of all the config files we currently expect
@@ -1,3 +1,3 @@
1
1
  module Sct
2
- VERSION = "1.0.8"
2
+ VERSION = "1.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sct
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reshad Farid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-17 00:00:00.000000000 Z
11
+ date: 2021-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colored
@@ -215,6 +215,10 @@ files:
215
215
  - cluster/lib/cluster/commands_generator.rb
216
216
  - cluster/lib/cluster/module.rb
217
217
  - cluster/lib/cluster/runner.rb
218
+ - henk/lib/henk.rb
219
+ - henk/lib/henk/commands_generator.rb
220
+ - henk/lib/henk/module.rb
221
+ - henk/lib/henk/runner.rb
218
222
  - sct/lib/.DS_Store
219
223
  - sct/lib/sct.rb
220
224
  - sct/lib/sct/.DS_Store
@@ -250,6 +254,7 @@ post_install_message:
250
254
  rdoc_options: []
251
255
  require_paths:
252
256
  - cluster/lib
257
+ - henk/lib
253
258
  - sct/lib
254
259
  - sct_core/lib
255
260
  required_ruby_version: !ruby/object:Gem::Requirement