Class: Cenvup::CLI

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

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



3
4
5
6
7
8
9
# File 'lib/cenvup/cli.rb', line 3

def initialize
  @conf = ::File.join(ENV['HOME'], ".cenvup/config.json")

  help_me if ARGV.count == 0
  help_me if ARGV[0] =~ /^(-h|--help|help)$/
  run
end

Instance Method Details

#config_check(cenvup_conf) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/cenvup/cli.rb', line 37

def config_check(cenvup_conf)
  if ::File.exist?(cenvup_conf)
    validate_conf(cenvup_conf)
  else
    puts "Could not locate cenvup.json at #{cenvup_conf}, launching config_init".yellow
    config_init(cenvup_conf)
  end
end

#config_init(cenvup_conf) ⇒ Object



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

def config_init(cenvup_conf)
  puts "Creating empty config at #{cenvup_conf}".green
  mkdir_conf = "mkdir #{::File.join(ENV['HOME'], ".cenvup")}"
  touch_conf = "touch #{cenvup_conf}"
  unless ::File.exist?(::File.join(ENV['HOME'], ".cenvup"))
    `#{mkdir_conf}`
  end
  `#{touch_conf}`

  prompt_config
end

#find_kitchenObject

WIP



48
49
50
51
52
53
54
55
# File 'lib/cenvup/cli.rb', line 48

def find_kitchen
  Find.find(ENV['HOME']) do |p|
    if p.match(/kitchen/)
      return p
      Find.prune
    end
  end
end

#from_file(env) ⇒ Object



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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/cenvup/cli.rb', line 160

def from_file(env)
  config = JSON.parse(File.read(@conf))

  file_name = "#{env}.json"

  if verify_kitchen_branch_status?(config['environments_path'])
    chef_server_url = ""
    org_name = ""
    if File.exist?(::File.join(config['environments_path'], ".chef/knife.rb"))
      File.open("#{::File.join(config['environments_path'], ".chef/knife.rb")}").each do |line|
        if line.match(/chef_server_url/)
          chef_server_url = line.split(" ")[1]
        end

        if line.match(/^org_name/)
          org_name = line.split(" ")[2]
        end
      end
    else
      exit 1
    end

    # puts chef_server_url
    if ::File.exist?(::File.join(config['environments_path'], "environments", file_name))
      cmd = "knife environment from file #{::File.join(config['environments_path'], "environments", file_name)}"
      puts "Kitchen on master and up to date.".green
      puts "Ready to upload #{::File.join(config['environments_path'], "environments", file_name)}".green
      puts "*** Command: #{cmd} ***".green
      prompt = ask('Do you want to proceed? (y/n)'.green)
      case prompt
      when "y"
        if verify_kitchen_branch_status?(config['environments_path'])
          puts "Validated that no update where performed.".green
          puts "Uploading #{file_name} to #{chef_server_url}".gsub("\#{org_name}", org_name[1..-2]).green
          puts `(cd #{config['environments_path']} && #{cmd})`.green
        else
          puts "Aborting, someone reuploaded meanwhile you where busy accepting!".red
          exit 1
        end
      when "n"
        puts "Cancelling.".red
        exit 1
      else
        puts "Invalid choice".red
        exit 1
      end
    else
      raise "Environment file #{::File.join(config['environments_path'], "environments", file_name)} could not be found".red
    end
  end
end

#help_meObject



226
227
228
229
230
231
232
233
234
235
# File 'lib/cenvup/cli.rb', line 226

def help_me
  puts 'Usage: cenvup (option)'
  puts ''
  puts 'Available options:'
  puts '  <ENV>             Upload <ENV> to chef server'
  puts '  locate_kitchen    Do a lookup for Kitchen from $HOME'
  puts '  help              This friendly help message'
  puts '  list              List available environments'
  exit 1
end

#list_environmentsObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cenvup/cli.rb', line 109

def list_environments
  config = JSON.parse(File.read(@conf))

  unless config['environments_path']
    prompt_config
    config = JSON.parse(File.read(@conf))
  end

  listing = ::Dir.entries(::File.join(config['environments_path'], "environments"))
  puts "Listing environments: ".green
  listing.each do |d|
    file = d.gsub(".json", "")
    puts " #{file}".green unless file =~ /^\./
  end
end

#locate_kitchenObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cenvup/cli.rb', line 57

def locate_kitchen
  puts "Trying to locate kitchen".green
  search = find_kitchen

  if search.nil? || search.empty?
    puts "Could not locate kitchen and environments_path is not setup in #{@conf}".red
    exit 1
  else
    config = {
      "environments_path" => search
    }
    File.open(@conf, 'w') do |f|
      f.puts JSON.pretty_generate(config)
    end

    puts "Kitchen found and added to #{@conf}".green
  end
end

#prompt_configObject



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
# File 'lib/cenvup/cli.rb', line 76

def prompt_config
  # puts "No config to your environments location was found.\nIf you are not using devtools-kitchen, please enter path manually.\nOtherwise you can try auto-discovery.\n".yellow
  puts "No config to your environments location was found.".yellow
  puts "1. Enter path manually".green
  # puts "2. Try discovery".green
  puts "0. Exit".green
  menu_choice = ask("Choice: ".green, Integer)

  case menu_choice
  when 1
    path = ask("Kitchen full path: ".green)
    if ::File.exist?(path.to_s)
      puts "Using #{path}".green
      config = {
        "environments_path" => path
      }
      File.open(@conf, 'w') do |f|
        f.puts JSON.pretty_generate(config)
      end
    else
      puts "Path does not exist. Exiting".red
      exit 1
    end
  when 2
    locate_kitchen
  when 0
    puts "Exiting.".red
    exit 1
  else
    puts "Wrong input detected".red
  end
end

#runObject



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/cenvup/cli.rb', line 212

def run
  config_check(@conf)

  case ARGV[0]
  when 'locate_kitchen'
    puts "WIP"
    # locate_kitchen
  when 'list'
    list_environments
  else
    from_file(ARGV[0])
  end
end

#validate_conf(config_file) ⇒ Object



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

def validate_conf(config_file)
  config = File.read(config_file)
  begin
    JSON.parse(config)
  rescue JSON::ParserError => e
    puts "cenvup.json is not a valid json : #{e}"
    nb_lines = `wc -l #{config_file}`.split(" ")[0]
    unless nb_lines.to_i > 2
      config_init(config_file)
    end
    run
  end
end

#verify_kitchen_branch_status?(kitchen) ⇒ Boolean

Returns:

  • (Boolean)


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
157
158
# File 'lib/cenvup/cli.rb', line 125

def verify_kitchen_branch_status?(kitchen)
  status = "git -C #{kitchen} status"
  fetch = "git -C #{kitchen} fetch"
  status_result = ""
  begin
    Timeout.timeout(10) do
      status_result = `#{fetch};#{status}`.yellow
    end
  rescue Timeout::Error
    puts "Command timeout.".red
    puts "Aborting.".red
    exit 1
  end

  if status_result.match(/On\sbranch\smaster\n/)
    if status_result.match(%r[Your\sbranch\sis\sup-to-date\swith\s'origin/master'])
      if status_result.match(/nothing\sto\scommit/)
        return true
      else
        puts "You have uncommited work.".red
        puts "Aborting.".red
        return false
      end
    else
      puts "On master branch but origin master is ahead. You should update your master branch!".red
      puts "Aborting.".red
      return false
    end
  else
    puts "Not on master branch.".red
    puts "Aborting.".red
    return false
  end
end