sct 1.0.9 → 1.2.0

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: a4e19d771e32524e38a9d685d161bdf57a17a71569536ce5af8fd51462b52960
4
- data.tar.gz: 610520f2b59b575ccc858c80af79cd1b40b470c403c849e34415559461e9b4ee
3
+ metadata.gz: d0986700d21895e4341fb5dfccf2ae8ea24dcda3fd62677733fcfa082396f410
4
+ data.tar.gz: 06d47615615d788cc402f099451b4efc833093f9030b624bfdf849b893c8b98b
5
5
  SHA512:
6
- metadata.gz: cd68fb3fd3e6c5ba9c4a91232cb83427697631385367eefff7a5477fd99e10f8c524f9d7ea4327c56d7ab31a93fca09e4ee610aad3b099ce14d26f7ab4777e76
7
- data.tar.gz: 2ab0d6e33f702ec64050126b05ee818e607ceae6b806a5ee91938295cf7e8e69824e3e3dacf41688e4f538041e67aaacfecd7c7708d168be3ce5311ef527e75b
6
+ metadata.gz: f778fc09644bd3f8a0ce76f891c9b744216643e61cc1592e654db260b84afd660fbcf6991484e2d2097f577a64b55a0f1fa832fe4a3226c2454e209be870b787
7
+ data.tar.gz: 3344106a0b57eb94b5cf82cc7e3e6872697fc95eb567e00a1a63149986d695db54e0a84fd8db3ad477394ec0a4620592dbf26381633d5dee218750b2ee94d597
@@ -26,7 +26,7 @@ module Cluster
26
26
 
27
27
  run "docker container prune -f"
28
28
 
29
- run_dc "up --detach"
29
+ run_dc "up --detach --remove-orphans --force-recreate --always-recreate-deps"
30
30
 
31
31
  if options.pull or options.build
32
32
  system "docker image prune -f"
@@ -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'
@@ -23,14 +23,27 @@ module Sct
23
23
  error "Private token not set"
24
24
  end
25
25
 
26
- if options.proactive_frame
27
- UI.important("Requesting to pull database for proactive frame")
28
- process_request = `curl -X POST -F token=#{private_token} -F ref=master -F variables[DB_PULL_FRAME]=true -F variables[REMOTE_BRANCH_NAME]=#{branch} #{pipeline_endpoint}`
29
- end
30
-
31
- if options.proactive_config
32
- UI.important("Requesting to pull database for proactive config")
33
- process_request = `curl -X POST -F token=#{private_token} -F ref=master -F variables[DB_PULL_CONFIG]=true -F variables[REMOTE_BRANCH_NAME]=#{branch} #{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
34
47
  end
35
48
  end
36
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?
@@ -55,7 +46,7 @@ module Sct
55
46
  }
56
47
 
57
48
  if options.pull
58
- return unless dc_dev "pull"
49
+ return unless dc_dev "pull", env
59
50
  end
60
51
 
61
52
  if options.build
@@ -66,11 +57,30 @@ 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, service_spec in services
61
+ return unless dc "rm --stop --force #{service}"
62
+ end
63
+
64
+ if services.length == 1
65
+ service, service_spec = services.first
70
66
 
71
- dc_dev "run --rm --service-ports --name #{container} #{service} #{command}", env
67
+ container = service_spec["container_name"]
68
+ command = service_spec["command"] || ""
72
69
 
73
- dc "up --detach #{service}"
70
+ dc_dev "run --rm --service-ports --name #{container} #{service} #{command}", env
71
+ else
72
+ begin
73
+ dc_dev "up --remove-orphans --force-recreate --always-recreate-deps", env
74
+ rescue Interrupt
75
+ # user pressed Ctrl+C, do nothing
76
+ end
77
+
78
+ dc_dev "down --remove-orphans", env
79
+ end
80
+
81
+ for service, service_spec in services
82
+ dc "up --detach #{service}"
83
+ end
74
84
  end
75
85
 
76
86
  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"]
@@ -43,6 +39,8 @@ module Sct
43
39
 
44
40
  user = options.root ? "root:root" : "$(id -u):$(id -g)"
45
41
 
42
+ puts "Attaching to shell in container #{container.bold}".green
43
+
46
44
  system "docker exec -it --user #{user} #{container} #{command}"
47
45
  end
48
46
 
@@ -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.9"
2
+ VERSION = "1.2.0"
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.9
4
+ version: 1.2.0
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-22 00:00:00.000000000 Z
11
+ date: 2022-02-17 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