Class: Pantry::PantrySet

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/set.rb

Instance Method Summary collapse

Instance Method Details

#get_cbk_list(options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/knife/set.rb', line 32

def get_cbk_list(options)
  chef_env =  rest.get("environments/#{options[:current_env]}")
  env_json = JSON.parse(chef_env.to_json)
  cbk_list = env_json['cookbook_versions']

  if cbk_list.has_key?(options[:cbk])
    if cbk_list[options[:cbk]] == "= " + options[:ver]
      return nil
    else
      cbk_list[options[:cbk]] = "= " + options[:ver]
    end
  end

  new_cbk_list = cbk_list
  env_json['cookbook_versions'] = new_cbk_list
  return env_json
end

#runObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chef/knife/set.rb', line 9

def run
  options = {
    cbk: name_args.first,
    ver: name_args[1],
    env: name_args.last
  }

  if options[:ver] == 'latest'
    available_versions = rest.get_rest("cookbooks/#{options[:cbk]}")
    options[:ver] = available_versions[options[:cbk]]['versions'][0]['version']
  end

  options[:env] = options[:env].split(',')
  options[:env].each {|x|
    options[:current_env] = x
    if set(get_cbk_list(options))
      puts "[Action Complete!]    #{options[:current_env]} => #{options[:ver]} => #{options[:cbk]}"
    else
      puts "[No Action!]    #{options[:current_env]} already using #{options[:cbk]} #{options[:ver]}"
    end
  }
end

#set(env_json) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/chef/knife/set.rb', line 50

def set(env_json)
  if env_json != nil
    rest.put_rest("environments/" + env_json['name'], env_json)
    return true
  else
    return false
  end
end