Module: KnifeSharp::Common::InstanceMethods

Defined in:
lib/knife-sharp/common.rb

Instance Method Summary collapse

Instance Method Details

#bot(message, config = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/knife-sharp/common.rb', line 107

def bot(message, config={})
  begin
    require "net/http"
    require "uri"
    uri = URI.parse("#{config["url"]}/#{config["channel"]}")
    notif = "chef: #{message} by #{config["username"]}"
    Net::HTTP.post_form(uri, { "message" => notif })
  rescue
    ui.error "Unable to notify via bot."
  end
end

#chef_serverObject



84
85
86
# File 'lib/knife-sharp/common.rb', line 84

def chef_server
  @chef_server ||= SharpServer.new.current_server
end

#cookbook_pathObject



54
55
56
# File 'lib/knife-sharp/common.rb', line 54

def cookbook_path
  @cookbook_path ||= [Chef::Config.send(:cookbook_path)].flatten.first
end

#data_bag_pathObject



58
59
60
# File 'lib/knife-sharp/common.rb', line 58

def data_bag_path
  @data_bag_path ||= [Chef::Config.send(:data_bag_path)].flatten.first
end

#ensure_branch_and_environment_provided!Object



36
37
38
39
40
41
# File 'lib/knife-sharp/common.rb', line 36

def ensure_branch_and_environment_provided!
  if @name_args.size != 2
    show_usage
    exit 1
  end
end

#ensure_branch_provided!Object



43
44
45
46
47
48
# File 'lib/knife-sharp/common.rb', line 43

def ensure_branch_provided!
  if @name_args.size != 1
    show_usage
    exit 1
  end
end

#ensure_correct_branch_provided!Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/knife-sharp/common.rb', line 23

def ensure_correct_branch_provided!
  # Checking current branch
  given_branch = @name_args.first

  Dir.chdir(sharp_config["global"]["git_cookbook_path"]) do
    current_branch = %x(git rev-parse --abbrev-ref HEAD).chomp
    if given_branch != current_branch
      ui.error "Git repo is actually on branch #{current_branch} but you want to align using #{given_branch}. Checkout to the desired one."
      exit 1
    end
  end
end

#environmentObject



50
51
52
# File 'lib/knife-sharp/common.rb', line 50

def environment
  @environment ||= @name_args.last
end

#environment_pathObject



66
67
68
# File 'lib/knife-sharp/common.rb', line 66

def environment_path
  @environment_path ||= [Chef::Config.send(:environment_path)].flatten.first
end

#ignore_list(component) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/knife-sharp/common.rb', line 119

def ignore_list(component)
  if sharp_config[chef_server] and sharp_config[chef_server]["ignore_#{component}"]
    sharp_config[chef_server]["ignore_#{component}"]
  else
    []
  end
end

#log_action(message) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/knife-sharp/common.rb', line 88

def log_action(message)
  # log file if enabled
  log_message = message
  log_message += " on server #{chef_server}" if chef_server
  logger.info(log_message) if sharp_config["logging"]["enabled"]

  # any defined notification method (currently, only hubot, defined below)
  if sharp_config["notification"]
    sharp_config["notification"].each do |carrier, data|
      skipped = Array.new
      skipped = data["skip"] if data["skip"]

      if data["enabled"] and !skipped.include?(chef_server)
        send(carrier, message, data)
      end
    end
  end
end

#loggerObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/knife-sharp/common.rb', line 70

def logger
  return @logger unless @logger.nil?

  begin
    log_file = sharp_config["logging"]["destination"] || "~/.chef/sharp.log"
    @logger = Logger.new(File.expand_path(log_file))
  rescue Exception => e
    ui.error "Unable to set up logger (#{e.inspect})."
    exit 1
  end

  @logger
end

#role_pathObject



62
63
64
# File 'lib/knife-sharp/common.rb', line 62

def role_path
  @role_path ||= [Chef::Config.send(:role_path)].flatten.first
end

#sharp_configObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/knife-sharp/common.rb', line 8

def sharp_config
  return @sharp_config unless @sharp_config.nil?

  config_file = File.expand_path("~/.chef/sharp-config.yml")

  begin
    @sharp_config = YAML::load_file(config_file)
  rescue Exception => e
    ui.error "Failed to load config file #{config_file}: #{e.message}"
    exit 1
  end

  @sharp_config
end