Class: Hastie::ReportPublisher

Inherits:
ServerReader show all
Defined in:
lib/hastie/report_publisher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ServerReader

#get_server_config, #read_config_file, source_root

Instance Attribute Details

#report_dirObject

Returns the value of attribute report_dir.



12
13
14
# File 'lib/hastie/report_publisher.rb', line 12

def report_dir
  @report_dir
end

Class Method Details



8
9
10
# File 'lib/hastie/report_publisher.rb', line 8

def self.banner
  "hastie publish <REPORT_DIR> <OPTIONS>"
end

Instance Method Details

#add_to_reports_fileObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/hastie/report_publisher.rb', line 125

def add_to_reports_file
  in_root do
    say_status "note", "modifying #{SERVER_REPORTS_FILE}"
    say_status "note", " to include #{options[:local]["report_id"]}"
    server_report_file = SERVER_REPORTS_FILE
    if !File.exists? server_report_file
      say_status "error", "Cannot find #{SERVER_REPORTS_FILE} file in server directory:", :red
    end
    ConfigFile.append(server_report_file, options[:local]["report_id"])
  end
end

#check_optionsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hastie/report_publisher.rb', line 29

def check_options
  all_valid = true
  required_server_options = ["reports_dir"]
  required_server_options.each do |option|
    if !options[:server] or !options[:server][option]
      say_status "error", "Missing #{option} option from server config", :red
      all_valid = false
    end
  end

  required_local_options = ["report_file", "report_id"]
  required_local_options.each do |option|
    if !options[:local] or !options[:local][option]
      say_status "error", "Missing #{option} option from local config", :red
      all_valid = false
    end
  end

  if !all_valid
    exit(1)
  end
end

#copy_data_directoryObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/hastie/report_publisher.rb', line 97

def copy_data_directory
  data_dir = File.join(report_dir, DATA_ROOT, options[:local]["report_id"])
  destination_dir = File.join(options[:server_root], DATA_ROOT)

  existing_destination_dir = File.join(destination_dir, options[:local]["report_id"])

  if File.exists? existing_destination_dir
    say_status "removing", existing_destination_dir, :yellow
    command = "rm -rf #{existing_destination_dir}"
    pid = Process.fork do
      exec(command)
    end
    Process.waitpid(pid)
  end

  if File.exists? data_dir
    say_status "publishing", data_dir
    # command = "cp -r #{data_dir} #{destination_dir}"
    # pid = Process.fork do
    #   exec(command)
    # end
    # Process.waitpid(pid)
    FileUtils.cp_r data_dir, destination_dir
  else
    say_status "warning", "report data directory not found #{data_dir}", :yellow
  end
end

#copy_report_fileObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hastie/report_publisher.rb', line 69

def copy_report_file
  report_filename = options[:local]["report_file"]
  local_report = File.join(report_dir, report_filename)
  destination_report = File.join(options[:server_root], options[:server]["reports_dir"], report_filename)

  if File.exists? destination_report
    say_status "removing", destination_report, :yellow
    command = "rm -f #{destination_report}"
    pid = Process.fork do
      exec(command)
    end
    Process.waitpid(pid)
  end

  if File.exists? local_report
    say_status "publishing", report_filename
    # command = "cp #{local_report} #{destination_report}"
    # pid = Process.fork do
    #   exec(command)
    # end
    # Process.waitpid(pid)
    FileUtils.cp local_report, destination_report
  else
    say_status "error", "Report file not found: #{report_filename}", :red
    exit(1)
  end
end

#create_lock_fileObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hastie/report_publisher.rb', line 57

def create_lock_file
  lock_file = File.join(self.destination_root, "lock.txt")
  if File.exists?(lock_file)
    say_status "error", "Another user is currently publishing to server", :red
    say_status "error", "Please wait", :red
    exit(1)
  end
  File.open(lock_file, 'w') do |file|
    file.puts Time.now.strftime('%Y-%m-%d %H:%M')
  end
end

#publish_with_jekyllObject

def update_git_repo in_root do say_status "note", "updating git repository" Grit::Git.git_timeout = 25 repo = Grit::Repo.new(".") # ensure we are on the server branch repo.git.native :checkout, {}, 'server' repo = Grit::Repo.new(".") if repo.head.name != "server" say_status "error", "Remote git not on server branch", :red say_status "error", "Please git checkout server", :red say_status "error", "Current branch: #Hastie::ReportPublisher.reporepo.headrepo.head.name", :red exit(1) end all_files = Dir.glob("./**") repo.add(all_files) repo.commit_all("update with report: #options[:local]["report_id"]") end end



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/hastie/report_publisher.rb', line 157

def publish_with_jekyll
  in_root do
    say_status "publishing", "updating server reports", :yellow
    config_file = File.expand_path(File.join(FileUtils.getwd, Hastie.watch_config_file))
    server_config_file = File.expand_path(File.join(FileUtils.getwd, Hastie.publish_config_file))
    if File.exists?(server_config_file)
      config_file = server_config_file
    end
    say_status "config", config_file, :yellow
    command = "jekyll --config #{config_file}"
    pid = Process.fork do
      exec(command)
    end
    Process.waitpid(pid)
  end
end

#read_report_fileObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hastie/report_publisher.rb', line 16

def read_report_file
  self.report_dir = File.expand_path(name)
  report_config_file = File.join(report_dir, Hastie.report_config_name)
  if !File.exists? report_config_file
    say "Cannot locate #{Hastie.report_config_name}."
    say "Current directory is not a report."
    exit(1)
  end
  # we get the report filename from this config file
  local_config = ConfigFile.load(report_config_file, :local)
  self.options = local_config.merge(self.options)
end

#remove_lock_fileObject



174
175
176
177
# File 'lib/hastie/report_publisher.rb', line 174

def remove_lock_file
  lock_file = File.join(self.destination_root, "lock.txt")
  system("rm -f #{lock_file}")
end

#set_destination_directoryObject



52
53
54
55
# File 'lib/hastie/report_publisher.rb', line 52

def set_destination_directory
  self.destination_root = File.join(options[:server_root])
  say_status "note", "root: #{self.destination_root}"
end