From af0587e894ffbedd95e3ef19aba6928814554243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E5=B1=B1=E5=90=B4=E5=BD=A6=E7=A5=96?= Date: Sat, 22 Dec 2018 11:54:48 +0800 Subject: [PATCH] move --- .gitignore | 10 + README.md | 2 + composer.json | 28 +++ composer.lock | 338 +++++++++++++++++++++++++++++++ example/db.php | 5 + example/deploy.php | 19 ++ example/runtime/build.db.php | 43 ++++ example/runtime/build.verify.php | 10 + example/test.php | 7 + phpunit.xml | 7 + src/Exception.php | 11 + src/Pull.php | 154 ++++++++++++++ 12 files changed, 634 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 example/db.php create mode 100644 example/deploy.php create mode 100644 example/runtime/build.db.php create mode 100644 example/runtime/build.verify.php create mode 100644 example/test.php create mode 100644 phpunit.xml create mode 100644 src/Exception.php create mode 100644 src/Pull.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b6be883 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +.* +!.gitignore +.idea/ +*.log +nbproject/ +.htaccess +vendor/ +composer.phar +!.phalcon.php +Vagrantfile \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..1204eee --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# bee-deploy +Bee框架部署组件 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f6f0bd8 --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "bee-framework/deploy", + "description": "Bee框架部署组件", + "type": "library", + "authors": [ + { + "name": "南山吴彦祖", + "email": "anoxia@engineer.com" + } + ], + "require": { + "php": "^7.1", + "adhocore/cli": "^0.3.2", + "bee-framework/util": "^1.0", + "guzzlehttp/guzzle": "^6.3", + "ext-json": "*" + }, + "autoload": { + "psr-4": { + "Bee\\Deploy\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Bee\\Deploy\\Tests\\": "tests/" + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..0519199 --- /dev/null +++ b/composer.lock @@ -0,0 +1,338 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "content-hash": "0c2a6dfc1329bbf6ab49c6d8983f299b", + "packages": [ + { + "name": "adhocore/cli", + "version": "0.3.2", + "source": { + "type": "git", + "url": "https://github.com/adhocore/php-cli.git", + "reference": "9dbab2581b1797b9b37caf83102ec0335e046f7d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/adhocore/php-cli/zipball/9dbab2581b1797b9b37caf83102ec0335e046f7d", + "reference": "9dbab2581b1797b9b37caf83102ec0335e046f7d", + "shasum": "" + }, + "require": { + "php": ">=7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Ahc\\Cli\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jitendra Adhikari", + "email": "jiten.adhikary@gmail.com" + } + ], + "description": "Command line interface library for PHP", + "keywords": [ + "PHP7", + "argument-parser", + "argv-parser", + "cli", + "cli-action", + "cli-app", + "cli-color", + "cli-option", + "cli-writer", + "command", + "console", + "console-app", + "php-cli", + "stream-input", + "stream-output" + ], + "time": "2018-09-06T15:32:22+00:00" + }, + { + "name": "bee-framework/util", + "version": "v1.0.1", + "source": { + "type": "git", + "url": "https://github.com/Anoxia/bee-util.git", + "reference": "c86452f22c7d76ff4af4651ae51becb981b97e2d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Anoxia/bee-util/zipball/c86452f22c7d76ff4af4651ae51becb981b97e2d", + "reference": "c86452f22c7d76ff4af4651ae51becb981b97e2d", + "shasum": "" + }, + "require": { + "ext-phalcon": "^3.2.0" + }, + "require-dev": { + "phalcon/ide-stubs": "*", + "phpunit/phpunit": "^7.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "Bee\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "description": "Bee框架通用类库", + "time": "2018-11-13T08:57:27+00:00" + }, + { + "name": "guzzlehttp/guzzle", + "version": "6.3.3", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/407b0cb880ace85c9b63c5f9551db498cb2d50ba", + "reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba", + "shasum": "" + }, + "require": { + "guzzlehttp/promises": "^1.0", + "guzzlehttp/psr7": "^1.4", + "php": ">=5.5" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.4 || ^7.0", + "psr/log": "^1.0" + }, + "suggest": { + "psr/log": "Required for using the Log middleware" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.3-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2018-04-22T15:46:56+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2016-12-20T10:07:11+00:00" + }, + { + "name": "guzzlehttp/psr7", + "version": "1.4.2", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/f5b8a8512e2b58b0071a7280e39f14f72e05d87c", + "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Schultze", + "homepage": "https://github.com/Tobion" + } + ], + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "request", + "response", + "stream", + "uri", + "url" + ], + "time": "2017-03-20T17:10:46+00:00" + }, + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06T14:39:51+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": "^7.1" + }, + "platform-dev": [] +} diff --git a/example/db.php b/example/db.php new file mode 100644 index 0000000..d2bcd5e --- /dev/null +++ b/example/db.php @@ -0,0 +1,5 @@ + 'http://134.175.219.113/', // 配置拉取接口地址 + 'token' => 'MTU0MDE5MzE3Ni0wMDAwMDA3MC1jMzI4YWFmMw==', // 身份令牌(来自部署用户组) +]; diff --git a/example/deploy.php b/example/deploy.php new file mode 100644 index 0000000..f05d6f6 --- /dev/null +++ b/example/deploy.php @@ -0,0 +1,19 @@ + [ + 'name' => 'build.db.php', + 'mapping' => [ + 'mysql.default' => 'w-dev-mysql-001', + 'redis.default' => 'w-dev-redis-001', + 'mq.default' => 'w-dev-rabiitmq-001', + ] + ], + + 'notify' => [ + 'name' => 'build.verify.php', + 'mapping' => [ + 'apple' => 'dev-apple-notify', + 'wx' => 'dev-wx-charge-notify' + ] + ] +]; diff --git a/example/runtime/build.db.php b/example/runtime/build.db.php new file mode 100644 index 0000000..81026f5 --- /dev/null +++ b/example/runtime/build.db.php @@ -0,0 +1,43 @@ + + array ( + 'default' => + array ( + 'host' => 'gz-cdb-k153onvv.sql.tencentcdb.com', + 'username' => 'api_user', + 'password' => 'nufang123!@#', + 'dbname' => 'fsxq_pub', + 'port' => 62401, + 'charset' => 'utf8mb4', + ), + ), + 'redis' => + array ( + 'default' => + array ( + 'host' => '192.168.0.13', + 'port' => 7833, + 'auth' => 'xiaoheiban123!@#', + ), + ), + 'mq' => + array ( + 'default' => + array ( + 'host' => '192.168.1.30', + 'port' => 5672, + 'login' => 'nufang', + 'password' => 'nufang123..', + 'vhost' => '/', + 'format' => 'igbinary', + ), + ), + 'log' => + array ( + 'default' => + array ( + 'app_key' => '132ceb11f8ab', + 'app_token' => '4a222981-6172-498c-aec1-06ff22d5c769', + ), + ), +); \ No newline at end of file diff --git a/example/runtime/build.verify.php b/example/runtime/build.verify.php new file mode 100644 index 0000000..41464af --- /dev/null +++ b/example/runtime/build.verify.php @@ -0,0 +1,10 @@ + + array ( + 'rechargeVerifyUrl' => 'https://buy.itunes.apple.com/verifyReceipt', + ), + 'wx' => + array ( + 'rechargeNotifyUrl' => 'https://api.fensixingqiu.com/v1.2/pay/notify', + ), +); \ No newline at end of file diff --git a/example/test.php b/example/test.php new file mode 100644 index 0000000..81ce413 --- /dev/null +++ b/example/test.php @@ -0,0 +1,7 @@ +run('pub'); diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..b105a9c --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,7 @@ + + + + tests + + + diff --git a/src/Exception.php b/src/Exception.php new file mode 100644 index 0000000..754acce --- /dev/null +++ b/src/Exception.php @@ -0,0 +1,11 @@ +server = $server; + $this->path = $path; + } + + /** + * 执行 + * + * @param $env + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function run($env) + { + // 获取指定配置元数据 + $config = $this->getMate($env); + + // 获取具体配置项,生成生成配置文件 + foreach ($config as $key => $item) { + $name = $item['name']; + $mapping = []; + + foreach ($item['mapping'] as $type => $id) { + $mapping[$type] = $this->getItem($id); + } + + $this->build($name, $mapping); + } + } + + /** + * 获取元数据 + * + * @param $alias + * @return mixed + * @throws \GuzzleHttp\Exception\GuzzleException + */ + private function getMate($alias) + { + $client = new Client( + [ + // Base URI is used with relative requests + 'base_uri' => $this->server['host'], + // You can set any number of default request options. + 'timeout' => 2.0, + ] + ); + + $response = $client->request( + 'GET', + "/runtime/mate/{$alias}", + [ + 'headers' => ['token' => $this->server['token']], + ] + ); + + return json_decode((string)$response->getBody(), true); + } + + /** + * 获取指定项数据 + * + * @param $alias + * @return string + * @throws \GuzzleHttp\Exception\GuzzleException + */ + private function getItem($alias) + { + $client = new Client( + [ + // Base URI is used with relative requests + 'base_uri' => $this->server['host'], + // You can set any number of default request options. + 'timeout' => 2.0, + ] + ); + + $response = $client->request( + 'GET', + "/runtime/item/{$alias}", + [ + 'headers' => ['token' => $this->server['token']], + ] + ); + + return json_decode((string)$response->getBody(), true); + } + + /** + * 获取并构建配置文件 + * + * @param $name + * @param $mapping + */ + private function build($name, array $mapping) + { + // 创建runtime目录 + is_dir($this->path) or mkdir($this->path, 0755); + + $config = []; + + foreach ($mapping as $key => $item) { + $path = explode('.', $key); + $pointer = &$config; + + foreach ($path as $level) { + if (!isset($pointer[$level])) { + $pointer[$level] = []; + } + + $pointer = &$pointer[$level]; + } + + $pointer = $item; + } + + $filename = $this->path . '/' . $name; + $content = '