Class: JenkinsShell

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

Defined Under Namespace

Classes: Error, OS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ JenkinsShell

Returns a new instance of JenkinsShell.



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
# File 'lib/jenkins_shell.rb', line 95

def initialize(params)
    @cookie = nil
    @crumb = nil
    @int_cwd = "."

    creds = "(([^:]+)(:(.+))?@)?"
    host = "([^:/]+)"
    path = "(/(.+))?"
    port = "(:([0-9]+))?"
    prot = "(https?)://"

    params["host"].match(
        /^#{prot}#{creds}#{host}#{port}#{path}/
    ) do |m|
        @host = m[6]
        @password = m[5]
        @path = m[10]
        @port = m[8]
        case m[1]
        when "http"
            @ssl = false
        when "https"
            @ssl = true
        end
        @username = m[3]
    end

    @os = params["os"]
    @password ||= params["password"]
    @path ||= params["path"]
    @path = "/#{@path}" if (@path)
    @port ||= params["port"] || 8080
    @ssl ||= params["ssl"] || false
    @username ||= params["username"]

    # Initialize @cwd
    @cwd = pwd
end

Instance Attribute Details

Returns the value of attribute cookie.



6
7
8
# File 'lib/jenkins_shell.rb', line 6

def cookie
  @cookie
end

#crumbObject (readonly)

Returns the value of attribute crumb.



7
8
9
# File 'lib/jenkins_shell.rb', line 7

def crumb
  @crumb
end

#cwdObject (readonly)

Returns the value of attribute cwd.



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

def cwd
  @cwd
end

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/jenkins_shell.rb', line 9

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



10
11
12
# File 'lib/jenkins_shell.rb', line 10

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



11
12
13
# File 'lib/jenkins_shell.rb', line 11

def port
  @port
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#cd(dir = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jenkins_shell.rb', line 14

def cd(dir = nil)
    return true if (dir.nil? || dir.empty?)

    new_cwd = pwd("#{@int_cwd}/#{dir}")

    return false if (new_cwd.empty? || (new_cwd == @cwd))

    @cwd = new_cwd
    @int_cwd = "#{@int_cwd}/#{dir}".gsub(%r{[^/]+/\.\.}, "")

    return true
end

#command(cmd, dir = @int_cwd) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jenkins_shell.rb', line 27

def command(cmd, dir = @int_cwd)
     if (@username && (@cookie.nil? || @crumb.nil?))
    if (@username && (@cookie.nil? || @crumb.nil?))
        raise JenkinsShell::Error::LoginFailure.new
    end

    # Create Groovy script
    gs = [
        "println(\"",
        "#{
            case @os
            when JenkinsShell::OS.LINUX
                # FIXME
                # "cd #{dir} && #{cmd}"
                cmd
            when JenkinsShell::OS.WINDOWS
                "cmd /c cd #{dir.gsub(/\\/, "\\\\\\")} && #{cmd}"
            end
        }",
        "\".execute().text",
        ")"
    ].join

    # Make POST request and return command output
    xml = post("/script", "script=#{encode(gs)}")[1]
    output = xml.get_elements("html/body/div/div/pre")[1]
    return "" if (output.nil?)
    return output.text.strip
end

#loginObject



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

def 
    get("/login")
    post(
        "/j_acegi_security_check",
        [
            "j_username=#{encode(@username)}",
            "j_password=#{encode(@password)}",
            "Submit=log+in"
        ].join("&")
    )
    get("/")
end

#pwd(dir = @int_cwd) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/jenkins_shell.rb', line 184

def pwd(dir = @int_cwd)
    case @os
    when JenkinsShell::OS.LINUX
        return command("pwd", dir)
    when JenkinsShell::OS.WINDOWS
        command("dir", dir).match(/^\s+Directory of (.+)/) do |m|
            return m[1]
        end
    end
    return ""
end