OSDN Git Service

6be106b777b0ea9dcfcc400a66b0fb831396a6f9
[twpd/master.git] / do.md
1 ---
2 title: Do gem
3 category: Ruby libraries
4 layout: 2017/sheet
5 ---
6
7 ### About
8 {: .-intro}
9
10  * [DAddYE/do](https://github.com/DAddYE/do)
11
12 ### Connection
13
14     server = DO::Server.new('srv1', 'srv1.domain.local', 'root', :key => 
15         %w[srv1.pem]
16     
17 ### Run
18     server.run 'uname'
19     # root@srv1 ~ # uname
20     # Linux
21     
22     server.run 'uname', '-a'
23     # root@srv1 ~ # uname -a
24     # Linux srv1.lipsiasoft.net 2.6.18-194.32.1.el5  x86_64 x86_64 x86_64 GNU/Linux
25     
26     server.run 'mysqladmin -u root -p password "oldone"', 'newpassword'
27     # root@srv1 ~ # mysqladmin -u root -p password 'oldone'
28     # Enter password: oldone
29     # mysqladmin: connect to server at 'localhost' failed
30     # error: 'Access denied for user 'root'@'localhost' (using password: YES)'
31     
32 ### Files
33
34     server.exist?('~/.ssh')
35     # root@srv1 ~ # test -e ~/.ssh && echo True
36     # => true
37     
38     server.read('/etc/redhat-release')
39     # root@srv1 ~ # cat /etc/redhat-release
40     # => "CentOS release 5.5 (Final)"
41
42 ### Upload/download
43
44     server.upload '/tmp/file', '/tmp/foo'
45     # root@srv1 ~ # upload from '/tmp/file' to '/tmp/foo'
46     
47     server.download '/tmp/foo', '/tmp/file2'
48     # root@srv1 ~ # download from '/tmp/foo' to '/tmp/file2'
49     
50 ### Replace
51
52     server.replace :all, 'new content', '/tmp/file'
53     # root@srv1 ~ # replace all in '/tmp/foo'
54     
55     server.read('/tmp/foo')
56     # root@srv1 ~ # cat /tmp/foo
57     # => "new content"
58     
59 ### Replace via regex
60
61     server.replace /content$/, 'changed content', '/tmp/foo'
62     # root@srv1 ~ # replace /content$/ in '/tmp/foo'
63     
64     server.read('/tmp/foo')
65     # root@srv1 ~ # cat /tmp/foo
66     # => "new changed content"
67     
68 ### Append
69
70     server.append('appended', '/tmp/foo')
71     # root@srv1 ~ # append to 'bottom' in '/tmp/foo'
72     
73     server.read('/tmp/foo')
74     # root@srv1 ~ # cat /tmp/foo
75     # => "new changed contentappended"
76     
77 ### Append to top
78
79     server.append('---', '/tmp/foo', :top)
80     # root@srv1 ~ # append to 'top' in '/tmp/foo'
81     
82     server.read('/tmp/foo')
83     # root@srv1 ~ # cat /tmp/foo
84     # => "---new changed contentappended"
85     
86 ### Prompt
87     
88     server.ask "Please choose"
89     # root@srv1 ~ # Please choose: foo
90     # => "foo"
91     
92     server.yes? "Do you want to proceed"
93     # root@srv1 ~ # Do you want to proceed? (y/n): y
94     # => 0
95     
96     server.wait
97     # Press ENTER to continue...