Class: SwarmClusterCliOpe::Cli

Inherits:
Thor
  • Object
show all
Includes:
ConfigurationConcern, LoggerConcern, StackSyncConcern, ThorConfigurationConcern, Thor::Actions
Defined in:
lib/swarm_cluster_cli_ope/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LoggerConcern

#logger

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/swarm_cluster_cli_ope/cli.rb', line 11

def self.exit_on_failure?
  true
end

Instance Method Details

#cp(src, dest) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/swarm_cluster_cli_ope/cli.rb', line 153

def cp(src, dest)
  cfgs.env(options[:environment]) do |cfgs|

    cfgs.stack_name = options[:stack_name] || cfgs.stack_name

    #identifico quale dei due è il servizio e quale la path
    if src.match(/^(.*)\:/)
      service_name = Regexp.last_match[1]
      remote = src.match(/\:(.*)$/)[1]
      local = dest
      execute = :pull
    else
      service_name = dest.match(/^(.*)\:/)[1]
      remote = dest.match(/\:(.*)$/)[1]
      local = src
      execute = :push
    end


    cmd = SyncConfigs::Copy.new(cfgs, {
      service: service_name,
      how: 'copy',
      configs: {
        local: local,
        remote: remote
      }
    })

    puts "COMPLETATO" if cmd.send(execute)

  end
end

#installObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/swarm_cluster_cli_ope/cli.rb', line 24

def install
  #contolliamo se presente la configurazione base nella home
  if Configuration.exist_base?
    say "Configurazione già presente"
  else
    #se non presente allora chiediamo le varie configurazioni
    say "Ricordarsi di poter raggiungere i server che verranno inseriti"
    lista = []
    loop do
      connection_name = ask("Aggiungi un server alla lista dei server Manager(inserire uri: ssh://server | unix:///socket/path:")
      result = Node.info(connection_name)
      node = Node.new(name: result.Name, connection_uri: connection_name)
      say "Aggiungo #{node.name} che si connette con DOCKER_HOST=#{node.connection_uri}"
      lista << node
      break if no? "Vuoi inserire altri server?[n,no]"
    end
    #scriviamo le varie configurazioni
    cfg = cfgs
    cfg.nodes = lista
    cfg.save_base_cfgs
  end

end

#mc(service_name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/swarm_cluster_cli_ope/cli.rb', line 73

def mc(service_name)
  cfgs.env(options[:environment]) do |cfgs|
    stack_name = options[:stack_name] || cfgs.stack_name
    # Disabilito output della libreria
    MakeMakefile::Logging.instance_variable_set(:@logfile, File::NULL)
    unless find_executable 'mc'
      puts "Non hai installato MC"
      exit 0
    end

    begin
      container = Models::Container.find_by_service_name(service_name, stack_name: stack_name)

      server = container.node.hostname

      # Creo container ssh
      #     DOCKER_HOST=ssh://swarm_node_1 docker run --rm -d -p 12222:22 \
      # --volumes-from sistemi-test_swarm_cluster_cli_wordpress.1.zbbz1xxh4vzzccndvs973jnuc \
      # sickp/alpine-sshd:7.5
      #
      cmd = container.docker_command
      cmd.base_suffix_command = ''
      shell_operation = cmd.command do |c|
        c.add("run --rm -d -p 42222:22 --volumes-from #{container.id} sickp/alpine-sshd:7.5")
      end

      puts "Creazione container #{shell_operation.string_command}"
      id_container = shell_operation.execute.raw_result[:stdout]
      puts "Container generato con id:#{id_container}"

      # eseguo tunnel verso nodo e container ssh
      socket_ssh_path = "/tmp/socket_ssh_#{id_container}"
      # ssh -f -N -T -M -S <path-to-socket> -L 13333:0.0.0.0:42222 <server>
      cmd_tunnel = ["ssh", "-f -N -T -M", "-S #{socket_ssh_path}", "-L 13333:0.0.0.0:42222", server].join(" ")
      puts "Apro tunnel"
      puts cmd_tunnel
      system(cmd_tunnel)

      # apro MC
      #     mc . sftp://root:[email protected]:13333
      mc_cmd = "mc . sftp://root:[email protected]:13333"
      puts "Apro MC"
      puts mc_cmd
      system(mc_cmd)
    ensure
      if socket_ssh_path
        # chiudo tunnel
        # ssh -S <path-to-socket> -O exit <server>
        close_tunnel_cmd = "ssh -S #{socket_ssh_path} -O exit #{server}"
        puts "Chiudo tunnel"
        # say close_tunnel_cmd
        ShellCommandExecution.new(close_tunnel_cmd).execute
      end

      if id_container
        # cancello container
        # docker stop  #{id_container}
        puts "Spengo container di appoggio"
        puts "docker stop  #{id_container}"
        cmd = container.docker_command
        cmd.base_suffix_command = ''
        stop_ssh_container = cmd.command do |c|
          c.add("stop #{id_container}")
        end
        stop_ssh_container.execute
      end

    end
  end
end

#rsync_binded_fromObject



215
216
217
218
219
# File 'lib/swarm_cluster_cli_ope/cli.rb', line 215

def rsync_binded_from
  if yes? "Attenzione, i dati locali verranno sovrascritti/cancellati?[y,yes]"
    rsync_binded(direction: :down, options: options)
  end
end

#rsync_binded_toObject



227
228
229
230
231
# File 'lib/swarm_cluster_cli_ope/cli.rb', line 227

def rsync_binded_to
  if yes? "ATTENZIONE, i dati remoti verranno sovrascritti/cancellati da quelli locali?[y,yes]"
    rsync_binded(direction: :up, options: options)
  end
end

#service_shell(service_name) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/swarm_cluster_cli_ope/cli.rb', line 191

def service_shell(service_name)
  cfgs.env(options[:environment]) do |cfgs|
    stack_name = options[:stack_name] || cfgs.stack_name
    container = Models::Container.find_by_service_name(service_name, stack_name: stack_name)

    cmd = container.docker_command
    cmd.base_suffix_command = ''
    shell_operation = cmd.command do |c|
      c.add("exec -it #{container.id} #{options[:shell]}")
    end

    say "Stai entrando della shell in #{options[:shell]} del container #{stack_name}->#{container.name}[#{container.id}]"
    system(shell_operation.string_command)
    say "Shell chiusa"
  end
end

#servicesObject



61
62
63
64
65
66
67
68
# File 'lib/swarm_cluster_cli_ope/cli.rb', line 61

def services
  cfgs.env(options[:environment]) do |cfgs|
    stack_name = options[:stack_name] || cfgs.stack_name
    Models::Service.all(stack_name: stack_name).each do |s|
      puts s.name
    end
  end
end

#stacksObject



52
53
54
55
56
# File 'lib/swarm_cluster_cli_ope/cli.rb', line 52

def stacks
  Models::Stack.all.each do |s|
    puts s.name
  end
end

#versionObject



234
235
236
# File 'lib/swarm_cluster_cli_ope/cli.rb', line 234

def version
  say VERSION
end