Class: K8sflow::Command::Run

Inherits:
Clitopic::Command::Base
  • Object
show all
Defined in:
lib/k8sflow/command/docker/run.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.aliasesObject

Returns the value of attribute aliases.



34
35
36
# File 'lib/k8sflow/command/docker/run.rb', line 34

def aliases
  @aliases
end

Class Method Details

.callObject



101
102
103
104
105
106
# File 'lib/k8sflow/command/docker/run.rb', line 101

def call
  pp options
  cmd = get_cmd(arguments)
  envs = env_vars(options)
  docker_run(cmd, envs, options)
end

.docker_run(cmd, envs, options) ⇒ Object



71
72
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
# File 'lib/k8sflow/command/docker/run.rb', line 71

def docker_run(cmd, envs, options)
  Docker.url = options[:docker_api]
  container_info =  {
    'Cmd' => cmd.split(" "),
    'Image' => "#{options[:registry]}:#{options[:tag]}",
    'Env' => envs.map{|k| "#{k[0]}=#{k[1]}"},
    'OpenStdin' => true
  }
  if !options[:detach]
    container_info["Tty"] = true
  end
  if options[:port]
    ctn_port, host_port = options[:port].split(":")
    container_info["HostConfig"] = {
      "PortBindings" => { "#{ctn_port}/tcp" => [{ "HostPort" => host_port.to_s}] }
    }
  end
  if options[:port_all] == true
    container_info["PublishAllPorts"] = true
  end
  pp container_info
  container = Docker::Container.create(container_info)
  container.start
  puts "container created with id: #{container.id}"
  if !options[:detach]
    puts "docker -H #{Docker.url} attach #{container.id}"
    exec("docker -H #{Docker.url} attach #{container.id}")
  end
end

.env_vars(options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/k8sflow/command/docker/run.rb', line 54

def env_vars(options)
  envs = {}
  if options.has_key?(:envs)
    options[:envs].each do |e|
      sp = e.split("=")
      key = sp[0..-2].join("=")
      value = sp[-1]
      envs[key] = value
    end
  end
  if options.has_key?(:heroku)
    envs.merge!( K8sflow::Utils::HerokuClient.envs(options[:heroku], options[:heroku_db]))
  end
  pp envs
  return envs
end

.get_cmd(args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/k8sflow/command/docker/run.rb', line 40

def get_cmd(args)
  if args.size == 0
    raise ArgumentError.new('no CMD')
  else
    cmd = args.join(" ").strip
    puts cmd
    if !@options[:aliases].nil? && @options[:aliases].is_a?(Hash)
      @aliases.merge!(@options[:aliases])
    end
    cmd = aliases[cmd] if aliases.has_key?(cmd)
    return cmd
  end
end