半人前技術者の成長記

挫折と妄想を膨らませながら成長するだろう技術者の日記

PHP Uncategorized バージョン管理

PhingでSSHする

投稿日:

サーバーへアプリケーションをリリースするために、Capistranoを使っている。
ただ、Rubyに詳しくないので何か問題が発生した場合に対処できない可能性が高い。
そこで、PHPベースのPhingを試してみました。

基本は、SSH、リポジトリへのアクセスできることが、最低限の要件です。

PHPのsshライブラリをインストール

# yum install –enablerepo=remi php-pecl-ssh2
# yum install –enablerepo=remi php-pear-phing
$ phing -version
Phing 2.5.0

SSHでログイン

作成したbuild.xmlは以下の通りです。
SSHでログインし、pwdコマンドを実行します。

1
2
3
4
5
6
<!--?xml version="1.0"?-->
<project name="sshtest" default="main">
  <target name="main">
    <ssh username="username" password="password" host="example.com" port="22" command="pwd">
  </ssh></target>
</project>

実行結果は、以下のようになります。

$ phing
Buildfile: /home/username/phing/build.xml

sshtest > main:

/home/username

BUILD FINISHED

Total time: 1.7562 second

次に、コマンドの実行結果を

1
2
3
4
5
6
7
<!--?xml version="1.0"?-->
<project name="sshtest" default="main">
  <target name="main">
    <ssh username="username" password="password" host="example.com" port="22" command="pwd" property="mypwd" display="false">
    <echo>The present working directory is ${mypwd}</echo>
  </ssh></target>
</project>

$ phing
Buildfile: /home/username/phing/build.xml

sshtest > main:

[echo] The present working directory is /home/hiro

BUILD FINISHED

Total time: 0.8402 seconds

SCPでファイル転送

先ほどのSSHを実行するのに加え、SCPでファイル転送し、リモート側のlsの結果を取得し、表示する場合のbuild.xmlは以下の通りです。

1
2
3
4
5
6
7
8
9
10
11
<!--?xml version="1.0"?-->
<project name="sshtest" default="main">
  <target name="main">
    <ssh username="username" password="password" host="example.com" port="22" command="pwd" property="mypwd" display="false">
    <echo>The present working directory is ${mypwd}</echo>
  </ssh></target>
  <target name="scp">
     <scp username="username" password="password" host="example.com" port="22" todir="./" file="test.txt">
     <ssh username="username" password="password" host="example.com" port="22" command="ls -l">
  </ssh></scp></target>
</project>

以下が、上記のXMLの実行結果になります。

$ phing scp
Buildfile: /home/hiro/phing/build.xml

sshtest > scp:

[scp] Copied 1 file(s) to ‘example.com’
合計 186456
-rw-rw-r– 1 username username 16 6月 2 13:00 test.txt
BUILD FINISHED

Total time: 2.8660 seconds

以上のことから、リモート側にログインしてリポジトリからソースコードを取得し、
自動でデプロイすることも可能と言える下調べができました。
次は、デプロイを行えるように書いてみたいと思います。
また、PHPのライブラリにAmazonのS3に接続用ライブラリもあるようなので、
バックアップなどのPHPでAmazonS3にファイルを転送するなども試してみたいと思います。

<参考>
http://www.hashbangcode.com/blog/using-ssh-and-scp-phing-603.html

pc

pc

-PHP, Uncategorized, バージョン管理
-, , ,

執筆者:

関連記事

no image

クラスの宣言

新たなクラス(自分で作ったクラス)を作ることを「クラスを宣言する」と言う. クラスの宣言の仕方 class MyClass{ ・・・ } classは,予約語で,クラスの宣言の始まりを示す. *予約語 …

no image

人工の心

人工無能で心を表現しようとした場合に何をしたらいいか. 今までは、人の心について研究されたことを参考にそれを再現するアルゴリズムを構築すれば、良いと思っていた. しかし、考え方を変えて適当なことを返す …

no image

新Mac Book

新しいMac Bookが出た! 今,会社支給のMac Bookを使っているが,重い! 新しいMac Bookは,約500gほど軽くなって2kgだそうです. ちょっと厚い本1冊分くらいは軽くなったかな. …

no image

九十九電気

九十九電気が,民事再生法を出してから1ヶ月くらい経つと思います. 秋葉原で老舗のパーツショップが無くなるかもしれないというのは,個人的には衝撃でした. 最近の秋葉原は,観光地になっているという面がある …

no image

PHPでファイルアップロード時に気をつける点

ファイルアップロード処理を作ることになったので、 ファイルサイズやファイルの形式以外に気をつける点が無いか、 気になったので調べた結果をまとめて置きます。 拡張子のチェック(アプリケーション) マジッ …

右上部広告

S