Module: Chef::Knife::StashBase

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chef/knife/stash_base.rb', line 14

def self.included(includer)
   includer.class_eval do

     deps do
       require 'readline'
       require 'chef/json_compat'
     end

     unless defined? $default
       $default = Hash.new
     end

     option :noop,
       :long => "--noop",
       :description => "Perform no modifying operations",
       :boolean => false

     option :stash_username,
       :short => "-u USERNAME",
       :long => "--stash-username USERNAME",
       :description => "The username for Stash"
     $default[:stash_username] = ENV['USER']

     option :stash_password,
       :short => "-p PASSWORD",
       :long => "--stash-password PASSWORD",
       :description => "The password for Stash"

 		option :stash_hostname,
 			:long => "--stash-hostname HOSTNAME",
 			:description => "The hostname for Stash"
   end
end

Instance Method Details

#display_stash_error(message, response) ⇒ Object



48
49
50
51
52
53
# File 'lib/chef/knife/stash_base.rb', line 48

def display_stash_error(message,response)
  ui.fatal message
  JSON.parse(response.body)['errors'].each do |error|
    ui.fatal "#{error['context'] ? error['context'] + ": " : "" }#{error['message']}"
  end
end

#get_all_values(stash, url, response) {|| ... } ⇒ Object

Yields:

  • ()


55
56
57
58
59
60
61
62
63
# File 'lib/chef/knife/stash_base.rb', line 55

def get_all_values(stash,url,response)
  r = JSON.parse(response.body)
  yield r['values']
  until r['isLastPage']
    response = stash.get url, { :start => r['nextPageStart'] }
    r = JSON.parse(response.body)
    yield r['values']
  end
end

#get_config(key) ⇒ Object



65
66
67
68
69
70
# File 'lib/chef/knife/stash_base.rb', line 65

def get_config(key)
	key = key.to_sym
	rval = config[key] || Chef::Config[:knife][key] || $default[key]
	Chef::Log.debug("value for config item #{key}: #{rval}")
	rval
end

#get_repo_https_url(project_key, repo) ⇒ Object



87
88
89
# File 'lib/chef/knife/stash_base.rb', line 87

def get_repo_https_url(project_key,repo)
  "https://#{get_config(:stash_username)}@#{get_config(:stash_hostname)}/scm/#{project_key}/#{repo}.git"
end

#get_repo_ssh_url(project_key, repo) ⇒ Object



91
92
93
# File 'lib/chef/knife/stash_base.rb', line 91

def get_repo_ssh_url(project_key,repo)
  "ssh://git@#{get_config(:stash_hostname)}:7999/#{project_key}/#{repo}.git"
end

#get_stash_connectionObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef/knife/stash_base.rb', line 72

def get_stash_connection
   config[:stash_hostname] = ask("Stash Hostname: ") { |q| q.echo = "*" } unless get_config(:stash_hostname)
   config[:stash_username] = ask("Stash Username for #{get_config(:stash_hostname)}: ") { |q| q.echo = "*" } unless get_config(:stash_username)
   config[:stash_password] = ask("Stash Password for #{get_config(:stash_username)}: ") { |q| q.echo = "*" } unless get_config(:stash_password)
   
   connection = Faraday.new(:url => "https://#{get_config(:stash_hostname)}", :ssl => {:verify => false}) do |faraday|
     faraday.request  :url_encoded # form-encode POST params
     #faraday.response :logger      # log requests to STDOUT
     faraday.adapter  :net_http    # make requests with Net::HTTP
   end
   connection.basic_auth(get_config(:stash_username),get_config(:stash_password))
   connection.url_prefix = "https://#{get_config(:stash_hostname)}/rest/api/1.0"
   connection
end