Class: K8sflow::Docker::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.



37
38
39
# File 'lib/k8sflow/command/docker/run.rb', line 37

def aliases
  @aliases
end

Class Method Details

.callObject



110
111
112
113
114
115
# File 'lib/k8sflow/command/docker/run.rb', line 110

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

.docker_run(cmd, envs, options) ⇒ Object



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

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/k8sflow/command/docker/run.rb', line 57

def env_vars(options)
  envs = {}
  if options.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.key?(:varager_api) && !options[:varager_api].empty?
    uri = options[:varager_api]
    token = options[:varager_token]
    if uri && token
      client = K8sflow::Utils::Varager.new(token, uri)
      envs.merge!(client.envs(options[:app], false))
    end
  end
  pp envs
  return envs
end

.get_cmd(args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/k8sflow/command/docker/run.rb', line 43

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