envirobly 1.3.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2991e127dfd64a68b47720bf70561343e3b7107304d9f5da9c1423cd4a3f0105
4
- data.tar.gz: 8d80a93f0af7427bd44668aaf47b68598ae9a62abd27168a35b52e9102cf9bf2
3
+ metadata.gz: 826c2cc2bbd5fe59d2e90e2ce7860025c327794a214735b08090b91008b3908e
4
+ data.tar.gz: d3390a6c48429e76a1cd1bc2f5a1d00a237996d63398f9cd521f512b0bc09964
5
5
  SHA512:
6
- metadata.gz: ab2ba4141f8bbc95fa907bdbfe78d30a9b1600862c9a97a34412de4bc295a4db6a8d46d52d0c97bc744792b15d57b051cf467b7d638287fb2c23e26593832e8f
7
- data.tar.gz: d9a69cb24f0d30fa89bdef0dfd06bfb07c78bc99f9fd32e9b2a2fbcfb136f81e1d72adfc39548b834b1c9ebe499fc6bb429e0dd5f9eeb1272448cf21f0018f26
6
+ metadata.gz: 74d0637bc3e704e07e2927f37ebbd68cc8c694bf1269f2a7d73536480798aad3985c91320654dac18b14532399d744820c4075d87f9cf7d072099db5908c264a
7
+ data.tar.gz: '08e97323c8c3ceffa549e64f5580d2edd33ebb6dac899a83773f92ae599ae33a645980ded7bfa3d93dfa019c0492a5314287a844cb64c5fee409a9cdb2b56cbc'
@@ -126,6 +126,27 @@ class Envirobly::Cli::Main < Envirobly::Base
126
126
  method_option :shell, type: :string
127
127
  method_option :user, type: :string
128
128
  def exec(service_name, *command)
129
- Envirobly::ContainerShell.new(service_name, command, options).connect
129
+ Envirobly::ContainerShell.new(service_name, options).exec(command)
130
+ end
131
+
132
+ desc "rsync [SERVICE_NAME:]SOURCE_PATH [SERVICE_NAME:]DESTINATION_PATH", <<~TXT
133
+ Synchronize files between you and your service's data volume.
134
+ TXT
135
+ method_option :account_id, type: :numeric
136
+ method_option :project_id, type: :numeric
137
+ method_option :project_name, type: :string
138
+ method_option :environ_name, type: :string
139
+ method_option :args, type: :string, default: "-avzP"
140
+ def rsync(source, destination)
141
+ service_name = nil
142
+
143
+ [ source, destination ].each do |path|
144
+ if path =~ /\A([a-z0-9\-_]+):/i
145
+ service_name = $1
146
+ break
147
+ end
148
+ end
149
+
150
+ Envirobly::ContainerShell.new(service_name, options).rsync(source, destination)
130
151
  end
131
152
  end
@@ -1,17 +1,25 @@
1
1
  class Envirobly::ContainerShell
2
- CMD_TEMLATE =
3
- "AWS_ACCESS_KEY_ID='%s' " +
4
- "AWS_SECRET_ACCESS_KEY='%s' " +
5
- "AWS_SESSION_TOKEN='%s' " +
6
- "ssh -i %s " +
7
- "envirobly-service@%s " +
8
- "-o StrictHostKeyChecking=accept-new " +
2
+ AWS_ENV = [
3
+ "AWS_ACCESS_KEY_ID='%s'",
4
+ "AWS_SECRET_ACCESS_KEY='%s'",
5
+ "AWS_SESSION_TOKEN='%s'"
6
+ ]
7
+ SSH = [
8
+ "ssh -i %s",
9
+ "-o StrictHostKeyChecking=accept-new",
10
+ "-o SendEnv=ENVIROBLY_SERVICE_INTERACTIVE_SHELL",
11
+ "-o SendEnv=ENVIROBLY_SERVICE_SHELL_USER",
9
12
  "-o ProxyCommand='aws ec2-instance-connect open-tunnel --instance-id %s --region %s'"
13
+ ]
14
+ USER_AND_HOST = "envirobly-service@%s"
10
15
 
11
- def initialize(service_name, command, options)
12
- @command = command
16
+ attr_reader :options, :service_name
17
+
18
+ def initialize(service_name, options)
19
+ @service_name = service_name
13
20
  @options = options
14
21
  commit = Envirobly::Git::Commit.new "HEAD"
22
+
15
23
  @params = {
16
24
  project: {
17
25
  account_id: options.account_id || Envirobly::Defaults::Account.new.id,
@@ -20,7 +28,7 @@ class Envirobly::ContainerShell
20
28
  },
21
29
  environ: { name: options.environ_name || commit.current_branch },
22
30
  service: { name: service_name },
23
- instance: { slot: options.instance_slot }
31
+ instance: { slot: options.instance_slot || 0 }
24
32
  }
25
33
 
26
34
  if options.project_name.blank? && options.account_id.blank? && options.project_id.blank?
@@ -28,39 +36,77 @@ class Envirobly::ContainerShell
28
36
  end
29
37
  end
30
38
 
31
- def connect
32
- api = Envirobly::Api.new
33
- response = api.create_service_shell_connection @params
34
- ssh_params = response.object
35
-
36
- Tempfile.create do |tempkey|
37
- tempkey.write ssh_params.fetch("instance").fetch("private_key")
38
- tempkey.flush
39
-
40
- cmd = sprintf(
41
- CMD_TEMLATE,
42
- ssh_params.fetch("open_tunnel_credentials").fetch("access_key_id"),
43
- ssh_params.fetch("open_tunnel_credentials").fetch("secret_access_key"),
44
- ssh_params.fetch("open_tunnel_credentials").fetch("session_token"),
45
- tempkey.path,
46
- ssh_params.fetch("instance").fetch("private_ipv4"),
47
- ssh_params.fetch("instance").fetch("aws_id"),
48
- ssh_params.fetch("region")
39
+ def exec(command = nil)
40
+ with_private_key do
41
+ system join(env_vars, ssh, user_and_host, command)
42
+ end
43
+ end
44
+
45
+ def rsync(source, destination)
46
+ with_private_key do
47
+ system join(
48
+ env_vars,
49
+ %(rsync #{options.args} -e "#{ssh}"),
50
+ source.sub("#{service_name}:", "#{user_and_host}:"),
51
+ destination.sub("#{service_name}:", "#{user_and_host}:")
49
52
  )
53
+ end
54
+ end
55
+
56
+ private
57
+ def join(*parts)
58
+ parts.flatten.compact.join(" ")
59
+ end
60
+
61
+ def connect_data
62
+ @connect_data ||= begin
63
+ api = Envirobly::Api.new
64
+ api.create_service_shell_connection(@params).object
65
+ end
66
+ end
50
67
 
51
- if @options.shell.present?
52
- cmd = "ENVIROBLY_SERVICE_INTERACTIVE_SHELL='#{@options.shell}' #{cmd} -o SendEnv=ENVIROBLY_SERVICE_INTERACTIVE_SHELL"
68
+ def with_private_key
69
+ Tempfile.create do |file|
70
+ file.write connect_data.fetch("instance").fetch("private_key")
71
+ file.flush
72
+
73
+ @private_key_path = file.path
74
+
75
+ yield
53
76
  end
77
+ end
78
+
79
+ def env_vars
80
+ credentials = connect_data.fetch("open_tunnel_credentials")
81
+
82
+ result = sprintf(
83
+ join(AWS_ENV),
84
+ credentials.fetch("access_key_id"),
85
+ credentials.fetch("secret_access_key"),
86
+ credentials.fetch("session_token")
87
+ )
54
88
 
55
- if @options.user.present?
56
- cmd = "ENVIROBLY_SERVICE_SHELL_USER='#{@options.user}' #{cmd} -o SendEnv=ENVIROBLY_SERVICE_SHELL_USER"
89
+ if options.shell.present?
90
+ result = join "ENVIROBLY_SERVICE_INTERACTIVE_SHELL='#{options.shell}'", result
57
91
  end
58
92
 
59
- if @command.present?
60
- cmd = "#{cmd} #{@command.join(" ")}"
93
+ if options.user.present?
94
+ result = join "ENVIROBLY_SERVICE_SHELL_USER='#{options.user}'", result
61
95
  end
62
96
 
63
- system cmd
97
+ result
98
+ end
99
+
100
+ def ssh
101
+ sprintf(
102
+ join(SSH),
103
+ @private_key_path,
104
+ connect_data.fetch("instance").fetch("aws_id"),
105
+ connect_data.fetch("region")
106
+ )
107
+ end
108
+
109
+ def user_and_host
110
+ sprintf USER_AND_HOST, connect_data.fetch("instance").fetch("private_ipv4")
64
111
  end
65
- end
66
112
  end
@@ -1,3 +1,3 @@
1
1
  module Envirobly
2
- VERSION = "1.3.1"
2
+ VERSION = "1.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: envirobly
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Starsi