Class: Ciborg::Config

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Config

Returns a new instance of Config.



22
23
24
25
# File 'lib/ciborg/config.rb', line 22

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

Class Method Details

.from_file(yaml_file) ⇒ Object



165
166
167
168
169
170
# File 'lib/ciborg/config.rb', line 165

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



172
173
174
# File 'lib/ciborg/config.rb', line 172

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



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ciborg/config.rb', line 27

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



180
181
182
# File 'lib/ciborg/config.rb', line 180

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

#basic_auth_userObject



176
177
178
# File 'lib/ciborg/config.rb', line 176

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

#buildsObject



184
185
186
187
188
189
# File 'lib/ciborg/config.rb', line 184

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

#cc_menu_urlObject



80
81
82
# File 'lib/ciborg/config.rb', line 80

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

#displayObject



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

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



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ciborg/config.rb', line 96

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



48
49
50
# File 'lib/ciborg/config.rb', line 48

def github_ssh_key
  File.read(github_ssh_key_path)
end

#github_ssh_key_pathObject



40
41
42
# File 'lib/ciborg/config.rb', line 40

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

#github_ssh_pubkeyObject



52
53
54
# File 'lib/ciborg/config.rb', line 52

def github_ssh_pubkey
  File.read(github_ssh_pubkey_path)
end

#github_ssh_pubkey_pathObject



44
45
46
# File 'lib/ciborg/config.rb', line 44

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

#jenkins_urlObject



76
77
78
# File 'lib/ciborg/config.rb', line 76

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

#master_urlObject



72
73
74
# File 'lib/ciborg/config.rb', line 72

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

#node_attributes=(attributes) ⇒ Object



88
89
90
# File 'lib/ciborg/config.rb', line 88

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

#reloadObject



129
130
131
# File 'lib/ciborg/config.rb', line 129

def reload
  self.class.from_file(path)
end

#rss_url(job_name) ⇒ Object



84
85
86
# File 'lib/ciborg/config.rb', line 84

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

#saveObject



116
117
118
119
120
# File 'lib/ciborg/config.rb', line 116

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



64
65
66
# File 'lib/ciborg/config.rb', line 64

def server_ssh_key
  File.read(server_ssh_key_path)
end

#server_ssh_key_pathObject



56
57
58
# File 'lib/ciborg/config.rb', line 56

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

#server_ssh_pubkeyObject



68
69
70
# File 'lib/ciborg/config.rb', line 68

def server_ssh_pubkey
  File.read(server_ssh_pubkey_path)
end

#server_ssh_pubkey_pathObject



60
61
62
# File 'lib/ciborg/config.rb', line 60

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

#soloistrcObject



108
109
110
111
112
113
114
# File 'lib/ciborg/config.rb', line 108

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

#to_hashObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ciborg/config.rb', line 133

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



122
123
124
125
126
127
# File 'lib/ciborg/config.rb', line 122

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

#valid?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/ciborg/config.rb', line 92

def valid?
  errors.empty?
end