Class: Lobot::Config

Inherits:
Hashie::Dash
  • Object
show all
Defined in:
lib/lobot/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Config

Returns a new instance of Config.



20
21
22
23
# File 'lib/lobot/config.rb', line 20

def initialize(attributes = {})
  super
  self["node_attributes"] = Hashie::Mash.new(node_attributes)
end

Class Method Details

.from_file(yaml_file) ⇒ Object



163
164
165
166
167
168
# File 'lib/lobot/config.rb', line 163

def self.from_file(yaml_file)
  config = {:path => yaml_file}
  config.merge!(read_config(yaml_file)) if File.exists?(yaml_file)
  config.delete('keypair_name')
  self.new(config)
end

.read_config(yaml_file) ⇒ Object



170
171
172
# File 'lib/lobot/config.rb', line 170

def self.read_config(yaml_file)
  File.open(yaml_file, "r") { |file| YAML.load(file.read) }
end

Instance Method Details

#add_build(name, repository, branch, command) ⇒ Object



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

def add_build(name, repository, branch, command)
  build = {
    "name" => name,
    "repository" => repository,
    "branch" => branch,
    "command" => command,
    "junit_publisher" => true
  }
  self.node_attributes = self.node_attributes.tap do |config|
    config.jenkins.builds << build unless config.jenkins.builds.include?(build)
  end
end

#basic_auth_passwordObject



178
179
180
# File 'lib/lobot/config.rb', line 178

def basic_auth_password
  node_attributes[:nginx][:basic_auth_password]
end

#basic_auth_userObject



174
175
176
# File 'lib/lobot/config.rb', line 174

def basic_auth_user
  node_attributes[:nginx][:basic_auth_user]
end

#buildsObject



182
183
184
185
186
187
# File 'lib/lobot/config.rb', line 182

def builds
  node_attributes[:jenkins][:builds].
    map { |build| build[:name] }    .
    map { |build| "    %-17s %s" % [build, rss_url(build)] }  .
    join("\n")
end

#cc_menu_urlObject



78
79
80
# File 'lib/lobot/config.rb', line 78

def cc_menu_url
  "#{jenkins_url}/cc.xml" if master
end

#displayObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/lobot/config.rb', line 144

def display
  <<OOTPÜT.gsub(/(\S)\s+$/, '\1').gsub(/^\./, '')
-- ciborg configuration --
  Instance ID:        #{instance_id}
  IP Address:         #{master}
  Instance size:      #{instance_size}
.
  Builds:
#{builds}
.
  Web URL:            #{master_url}
  User name:          #{basic_auth_user}
  User password:      #{basic_auth_password}
.
  CC Menu URL:        #{cc_menu_url}
.
OOTPÜT
end

#errorsObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/lobot/config.rb', line 94

def errors
  messages = []
  if node_attributes.has_key?("jenkins")
    unless node_attributes.jenkins.has_key?("builds")
      messages << "[:node_attributes][:jenkins][:builds]"
    end
  else
    messages << "[:node_attributes][:jenkins]"
  end
  messages.map{ |path| "your config file does not have a #{path} key" }
end

#github_ssh_keyObject



46
47
48
# File 'lib/lobot/config.rb', line 46

def github_ssh_key
  File.read(github_ssh_key_path)
end

#github_ssh_key_pathObject



38
39
40
# File 'lib/lobot/config.rb', line 38

def github_ssh_key_path
  File.expand_path(self["github_ssh_key"]) if self["github_ssh_key"]
end

#github_ssh_pubkeyObject



50
51
52
# File 'lib/lobot/config.rb', line 50

def github_ssh_pubkey
  File.read(github_ssh_pubkey_path)
end

#github_ssh_pubkey_pathObject



42
43
44
# File 'lib/lobot/config.rb', line 42

def github_ssh_pubkey_path
  github_ssh_key_path + ".pub" if self["github_ssh_key"]
end

#jenkins_urlObject



74
75
76
# File 'lib/lobot/config.rb', line 74

def jenkins_url
  "https://#{CGI.escape(basic_auth_user)}:#{CGI.escape(basic_auth_password)}@#{master}" if master
end

#master_urlObject



70
71
72
# File 'lib/lobot/config.rb', line 70

def master_url
  "https://#{master}" if master
end

#node_attributes=(attributes) ⇒ Object



86
87
88
# File 'lib/lobot/config.rb', line 86

def node_attributes=(attributes)
  self["node_attributes"] = Hashie::Mash.new(attributes)
end

#reloadObject



127
128
129
# File 'lib/lobot/config.rb', line 127

def reload
  self.class.from_file(path)
end

#rss_url(job_name) ⇒ Object



82
83
84
# File 'lib/lobot/config.rb', line 82

def rss_url(job_name)
  master_url + "/job/#{job_name}/rssAll" if master_url
end

#saveObject



114
115
116
117
118
# File 'lib/lobot/config.rb', line 114

def save
  return self unless path
  File.open(path, "w+") { |file| file.write(YAML.dump(JSON.parse(JSON.dump(to_hash)))) }
  self
end

#server_ssh_keyObject



62
63
64
# File 'lib/lobot/config.rb', line 62

def server_ssh_key
  File.read(server_ssh_key_path)
end

#server_ssh_key_pathObject



54
55
56
# File 'lib/lobot/config.rb', line 54

def server_ssh_key_path
  File.expand_path(self["server_ssh_key"]) if self["server_ssh_key"]
end

#server_ssh_pubkeyObject



66
67
68
# File 'lib/lobot/config.rb', line 66

def server_ssh_pubkey
  File.read(server_ssh_pubkey_path)
end

#server_ssh_pubkey_pathObject



58
59
60
# File 'lib/lobot/config.rb', line 58

def server_ssh_pubkey_path
  server_ssh_key_path + ".pub" if self["server_ssh_key"]
end

#soloistrcObject



106
107
108
109
110
111
112
# File 'lib/lobot/config.rb', line 106

def soloistrc
  {
    "recipes" => recipes,
    "cookbook_paths" => cookbook_paths,
    "node_attributes" => node_attributes.to_hash
  }
end

#to_hashObject



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/lobot/config.rb', line 131

def to_hash
  hash = super
  hash.delete("path")
  {
    "ssh_port" => ssh_port,
    "master" => master,
    "server_ssh_key" => server_ssh_key_path,
    "recipes" => recipes,
    "cookbook_paths" => cookbook_paths,
    "node_attributes" => node_attributes
  }.merge(hash)
end

#update(options = {}) ⇒ Object



120
121
122
123
124
125
# File 'lib/lobot/config.rb', line 120

def update(options = {})
  options.each_pair do |attr, value|
    self.send("#{attr}=", value)
  end
  save
end

#valid?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/lobot/config.rb', line 90

def valid?
  errors.empty?
end