根据changelist.txt 变更文件列表更新发布php项目

.
├── changelist.txt
├── index.php
├── index.php~
├── LineProcessor.php
└── output
    └── dist.sh
 

* changelist.txt

# chmod u+w ~/.ssh/known_hosts
# changelist
 src/app/service/registeredguide.php               
 src/app/view/registeredguide.php                   
 src/logic/registeredGuideLogic.php                 
 src/sqlmap/sql.config.inc                          
 # ------ *.tpl ----- 
 src/template/freecourse/showcourse.tpl             
 src/template/oldcoursemapping/showoldcoursemapping.tpl      
 src/template/picturelist/picturelist.tpl           
 src/template/registeredguide/addbackground.tpl     
 src/template/registeredguide/addqrcode.tpl         
 src/template/registeredguide/chooseeditpicture.tpl 
 src/template/registeredguide/choosepicture.tpl     
 src/template/registeredguide/editbackground.tpl    
src/template/registeredguide/editqrcode.tpl        
src/template/registeredguide/importqrcode.tpl      
src/template/registeredguide/searcheditpicture.tpl 
src/template/registeredguide/searchpicture.tpl     
src/template/registeredguide/searchqrcode.tpl      
src/template/registeredguide/showbackground.tpl    
src/template/registeredguide/showqrcode.tpl


linux ssh 免密码登录设置

* index.php

<?php
// usage: php7 index.php
// linux ssh 免密码登录设置
// @ref: https://blog.csdn.net/fareast_mzh/article/details/82049263

function __autoload($className) {
    include $className.'.php';
}

define('DIR_IN', ".");
define('DIR_OUT', "./output");
is_dir(DIR_OUT) || mkdir (DIR_OUT);

$p = new \LineProcessor(DIR_IN.'/changelist.txt');

$script = DIR_OUT.'/dist.sh';
$out = new \SplFileObject($script, 'w');

echo 'Generating script'.$script.' ...'.PHP_EOL;

$out->fwrite("#!/bin/bash\n");

$p->setHandler(function($cur) use ($out) {
    // filter
    $cur = trim($cur);
    if (empty($cur)) {
        return -1;
    }
    $len = strlen($cur);
    // comment
    if ($cur[0] === '#') {return -1;}

    // generate shell command
    $src = "/home/ubuntu/code/webdatamanager/";
    $dst = "ubuntu@172.16.0.224:/usr/ET/project/webdatamanager/";
    $cmd = sprintf("scp -o StrictHostKeyChecking=no %s%s %s%s", $src, $cur, $dst, $cur);
    // echo $cmd.PHP_EOL;
    $out->fwrite($cmd.PHP_EOL);

    return $len;
});

$p->run();

echo 'TODO: '.PHP_EOL;
echo 'bash '.$script.PHP_EOL;

 

* LineProcessor.php

<?php
 
class LineProcessor {
    /** @var \SplFileObject */
    private $file;
 
    /** @var int */
    private $linum;
 
    /** @var  callable */
    protected $handler;
 
    public function __construct(string $path) {
        $this->file = new \SplFileObject($path, 'r');
        $this->linum = 0;
    }
 
    /**
     * 试着读取$limit行, 对每一行执行$callback
     * @param callable $callback
     * @param int $limit default unlimited
     * @return int 实际读取的行数
     */
    public function forEach(callable $callback, int $limit = 0) {
        // unlimited
        if (0===$limit) {
            while ($this->file->valid()) {
                $line = $this->file->fgets();
                call_user_func($callback, $line, $this->linum);
                $this->linum++;
            }
            return 0;
        }
        for ($i = 0; $i < $limit && $this->file->valid(); $this->linum++) {
            $line = $this->file->fgets();
            if (!empty($line)) {
                call_user_func($callback, $line, $i);
                $i++;
            }   
        }
        return $i;
    }
 
    public function setHandler(callable $cb) {
        if (!$cb) {
            $cb = function($cur, $i) {
                printf("%d %s\n", $i, $cur);
            };
        }
        $this->handler = $cb;
    }
 
    public function run() {
        $this->forEach($this->handler);
    }
 
}
 

* 生成shell脚本

rm -rf  output/

php index.php

* run:

检查 ./output/dist.sh 然后执行:

bash ./output/dist.sh

ssh 发布代码 (php7) publish_shell

 

注意scp 要加参数

-o StrictHostKeyChecking=no

可以去除这样的确认prompt

The authenticity of host '172.16.0.220 (172.16.0.220)' can't be established.
ECDSA key fingerprint is d1:35:84:1e:26:34:1a:c0:7a:9f:b5:88:48:7a:89:19.
Are you sure you want to continue connecting (yes/no)?  

 

ssh免密码登录:

把本机的 ~/.ssh/id_rs.pub 文件追加到目的主机的 ~/.ssh/authorized_keys

 

~/.ssh/目录下的文件权限如下:

ubuntu@et-dev-mingzhanghui:~/code/php/distribution$ ls -l ~/.ssh/*
-rw------- 1 ubuntu ubuntu  799 Feb 20 03:47 /home/ubuntu/.ssh/authorized_keys
-rw------- 1 ubuntu ubuntu  399 Sep  3 04:22 /home/ubuntu/.ssh/authorized_keys~
-r-------- 1 ubuntu ubuntu 1679 Dec 11 07:03 /home/ubuntu/.ssh/id_rsa
-rw-rw-rw- 1 ubuntu ubuntu  401 Dec 25 01:30 /home/ubuntu/.ssh/id_rsa.pub
-rw-r--r-- 1 ubuntu ubuntu  408 Dec 11 07:03 /home/ubuntu/.ssh/id_rsa.pub~
-rw-r--r-- 1 ubuntu ubuntu 2884 Feb 20 03:52 /home/ubuntu/.ssh/known_hosts

 

php spl

http://php.net/manual/en/book.spl.php

http://php.net/manual/en/class.splfileobject.php