Class: Reemo::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/reemo.rb

Instance Method Summary collapse

Instance Method Details

#__print_versionObject



34
35
36
# File 'lib/reemo.rb', line 34

def __print_version
  puts(Reemo::VERSION)
end

#run_Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/reemo.rb', line 75

def run_()

  is_developping = options[:develop]

  if is_developping
    puts("==== DEVELOP OPTION IS ON ====")
  end

  # If configure file doesn't exist
  if !File.exists?(GLOBAL_CONFIG_YAML_PATH)
    STDERR.puts <<EOS
Error: Configure not found
You should configure server setting like the following.
$ reemo server-config --host=hogehoge.io --http-port=4567 --ssh-port=2222
EOS
    exit(1)
  end

  config_yaml_map = YAML::load_file(GLOBAL_CONFIG_YAML_PATH)

  # Load server setting from yaml
  host      = config_yaml_map["server"]["host"]
  http_port = config_yaml_map["server"]["http_port"]
  ssh_port  = config_yaml_map["server"]["ssh_port"]

  url = URI.parse("http://#{host}:#{http_port}/build") # TODO Hard code

  if !Dir.exists?(CONFIG_DIR_PATH)
    Dir.mkdir(CONFIG_DIR_PATH)
  end

  # Session ID (This will be filled)
  session_id = nil

  # Get old session ID
  old_session_id = File.exists?(OLD_SESSION_ID_FILE_PATH)? File.read(OLD_SESSION_ID_FILE_PATH) : nil

  Tempfile.open(){|tar_gz_f|
    workspace_tar_gz_path = tar_gz_f.path
    # Compress workspace to tar.gz
    Zlib::GzipWriter.open(tar_gz_f){|sgz|
      Archive::Tar::Minitar::Output.open(sgz){|tar|
        # Get ignore file path
        ignore_file_path   = IGNORE_FILE_NAME
        # Get all files
        all_file_paths     = Dir.glob("**/*", File::FNM_DOTMATCH).reject{|e| [".",".."].any?{|s| s== File::basename(e)}}
        # Ignore file exists or not
        if File.exists?(ignore_file_path)
          # Generate ignore
          ignore = Buff::Ignore::IgnoreFile.new(IGNORE_FILE_NAME)
          # Create file paths by using ignore
          file_paths = ignore.apply(all_file_paths)
        else
          # File paths are the same as all file paths because of no ignore
          file_paths = all_file_paths
        end
        # Archive files
        file_paths.each{|entry|
          Minitar.pack_file(entry, tar)
        }
      }
    }
    tar_gz_f.close()

    File.open(workspace_tar_gz_path, 'rb') do |f|
      # p f.read(10000)
      req = Net::HTTP::Post::Multipart.new(
          url.path,
          "file" => UploadIO.new(f, "application/tar+gzip", "dummy.tar.gz"),
          "old_session_id" => old_session_id
      )
      res = Net::HTTP.start(url.host, url.port) do |http|
        http.request(req)
      end

      session_id = res.entity
    end
  }

  if is_developping
    puts("Session ID: #{session_id}")
  end
  File.write(OLD_SESSION_ID_FILE_PATH, session_id)

  user_name = session_id
  ssh_command = "ssh -t -p #{ssh_port} #{user_name}@#{host}"
  system(ssh_command)

  open("http://#{host}:#{http_port}/output?session_id=#{session_id}"){|f|
    Tempfile.open(){|tmp_out_f|
      IO.copy_stream(f, tmp_out_f)
      tmp_out_f.close()

      if !File.zero?(tmp_out_f.path)
        if !Dir.exists?(OUTPUT_DIR_PATH)
          Dir.mkdir(OUTPUT_DIR_PATH)
        end

        # (from: http://www.itmedia.co.jp/help/tips/linux/l0418.html)
        system("tar xzf #{tmp_out_f.path} -C #{OUTPUT_DIR_PATH}/") # TODO: Not to use system
      end
    }
  }
end

#server_configObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/reemo.rb', line 42

def server_config()
  host      = options[:host]
  http_port = options[:http_port]
  ssh_port  = options[:ssh_port]

  # If global config directory doesn't exist yet
  if !Dir.exists?(GLOBAL_CONFIG_DIR_PATH)
    # Make the directory
    FileUtils.makedirs(GLOBAL_CONFIG_DIR_PATH)
  end

  # Get config yaml
  if File.exists?(GLOBAL_CONFIG_YAML_PATH)
    config_yaml_map = YAML::load_file(GLOBAL_CONFIG_YAML_PATH)
  else
    config_yaml_map = {}
  end

  # Set server config
  config_yaml_map["server"] = {
      "host"      => host,
      "http_port" => http_port,
      "ssh_port"  => ssh_port
  }

  # Write to yaml
  File.write(GLOBAL_CONFIG_YAML_PATH, YAML::dump(config_yaml_map))

  puts("Server configuration successful!")
end