Class: Etcenv::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/etcenv/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*argv) ⇒ Cli

Returns a new instance of Cli.



12
13
14
15
16
17
18
19
20
21
# File 'lib/etcenv/cli.rb', line 12

def initialize(*argv)
  @argv = argv
  @options = {
    etcd: URI.parse("http://localhost:2379"),
    format: DotenvFile,
    perm: 0600,
    mode: :oneshot,
  }
  parse!
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



23
24
25
# File 'lib/etcenv/cli.rb', line 23

def argv
  @argv
end

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/etcenv/cli.rb', line 23

def options
  @options
end

Instance Method Details

#etcdObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/etcenv/cli.rb', line 90

def etcd
  @etcd ||= Etcd.client(
    host: options[:etcd].host,
    port: options[:etcd].port,
    use_ssl: options[:etcd].scheme == 'https',
    ca_file: options[:etcd_ca_file],
    ssl_cert: options[:etcd_tls_cert],
    ssl_key: options[:etcd_tls_key],
  )
end

#oneshotObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/etcenv/cli.rb', line 101

def oneshot
  if argv.empty?
    $stderr.puts "error: no KEY specified. See --help for detail"
    return 1
  end
  env = argv.inject(nil) do |env, key|
    new_env = Environment.new(etcd, key).expanded_env
    env ? env.merge(new_env) : new_env
  end
  dump_env(env)

  0
end

#parse!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/etcenv/cli.rb', line 38

def parse!
  parser = OptionParser.new do |opts|
    opts.banner = "Usage: etcenv [options] key ..."

    opts.on("-h", "--help", "show this help") do
      puts opts
      exit 0
    end

    opts.on("--watch", "-w", "continuous update mode") do
      options[:mode] = :watch
    end

    opts.on("--daemon", "-d", "daemonize") do
      options[:daemonize] = true
    end

    opts.on("--pidfile PIDFILE", "-p PIDFILE", "pidfile to use when deamonizing") do |pidpath|
      options[:pidfile] = pidpath
    end

    opts.on("-o PATH", "--output PATH", "save to speciifed file") do |path|
      options[:output] = path
    end

    opts.on("-m MODE", "--mode MODE", "mode (permission) to use when creating --output file, in octal. Default: 0600") do |perm|
      options[:perm] = perm.to_i(8)
    end

    opts.on("--docker", "use docker env-file format instead of dotenv.gem format") do
      options[:format] = DockerenvFile
    end

    opts.on("--etcd URL", "URL of etcd to connect; Any paths are ignored.") do |url|
      options[:etcd] = URI.parse(url)
    end

    opts.on("--etcd-ca-file PATH", "Path to CA certificate file (PEM) of etcd server") do |path|
      options[:etcd_ca_file] = path
    end

    opts.on("--etcd-cert-file PATH", "Path to client certificate file (PEM) for etcd server") do |path|
      options[:etcd_tls_cert] = OpenSSL::X509::Certificate.new(File.read(path))
    end

    opts.on("--etcd-key-file PATH", "Path to private key file for client certificate for etcd server") do |path|
      options[:etcd_tls_key] = OpenSSL::PKey::RSA.new(File.read(path))
    end
  end
  parser.parse!(argv)
end

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/etcenv/cli.rb', line 25

def run
  parse!

  case options[:mode]
  when :oneshot
    oneshot
  when :watch
    watch
  else
    raise "[BUG] unknown mode"
  end
end

#watchObject



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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/etcenv/cli.rb', line 115

def watch
  if argv.empty?
    $stderr.puts "error: no KEY specified. See --help for detail"
    return 1
  end

  if options[:daemonize]
    $stderr.puts "Daemonizing"
    Process.daemon(nil, true)
    if options[:pidfile]
      File.write options[:pidfile], "#{$$}\n"
    end
  end

  envs = argv.map { |key| Environment.new(etcd, key) }

  watchers = envs.map { |env| Watcher.new(env, verbose: true) }

  dumper_ch = Queue.new
  dumper = Thread.new do
    loop do
      $stderr.puts "[dumper] dumping env"
      env = envs.inject(nil) do |result, env|
        new_env = env.expanded_env
        result ? result.merge(new_env) : new_env
      end
      dump_env(env)
      dumper_ch.pop
    end
  end
  dumper.abort_on_exception = true

  watchers.map do |watcher|
    Thread.new do
      watcher.auto_reload_loop do
        dumper_ch << true
      end
    end
  end

  loop { sleep 1 }
end