{"id":575,"date":"2014-12-26T17:43:47","date_gmt":"2014-12-26T09:43:47","guid":{"rendered":"https:\/\/marvelliu.space\/?p=575"},"modified":"2014-12-26T17:53:58","modified_gmt":"2014-12-26T09:53:58","slug":"%e4%bd%bf%e7%94%a8cloud-init%e5%ae%9e%e7%8e%b0%e8%99%9a%e6%8b%9f%e6%9c%ba%e4%bf%a1%e6%81%af%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/marvelliu.space\/?p=575","title":{"rendered":"\u4f7f\u7528cloud-init\u5b9e\u73b0\u865a\u62df\u673a\u4fe1\u606f\u7ba1\u7406"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48\u8981\u7528cloud-init<\/h2>\n<p>\u4e0d\u540c\u79cd\u7c7b\u7684\u8bbe\u5907VM\u542f\u52a8\u603b\u662f\u4e00\u4ef6\u975e\u5e38\u9ebb\u70e6\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982\u5b89\u5168\u8bbe\u5907\u6709WAF\u3001IPS\u7b49\uff0c\u6bcf\u79cd\u8bbe\u5907\u7684\u7f51\u7edc\u63a5\u53e3\u3001\u542f\u52a8\u811a\u672c\u4e92\u4e0d\u4e00\u6837\uff0c\u5373\u4fbf\u540c\u4e00\u79cd\u8bbe\u5907\uff0c\u5176\u4e3b\u673a\u540d\u3001\u7f51\u7edc\u5730\u5740\u7b49\u4e5f\u4e0d\u4e00\u6837\u3002\u90a3\u4e48\u5982\u4f55\u5bf9\u8fd9\u4e9bVM\u542f\u52a8\u8fc7\u7a0b\u8fdb\u884c\u7ba1\u7406\uff0c\u5e76\u5b8c\u6210\u6240\u6709\u6570\u636e\u7684\u914d\u7f6e\u5462\uff1f <\/p>\n<p>\u5728\u8fd9\u4e4b\u524d\uff0c\u6211\u7684\u5b9e\u4e60\u751f\u662f\u600e\u4e48\u505a\u7684\uff1a\u5c06\u4e00\u53f0VM\u7684\u7ba1\u7406\u53e3\u7f51\u7edc\u5730\u5740\u8bbe\u7f6e\u4e3a192.168.2.100\uff0c\u7136\u540e\u6bcf\u6b21\u542f\u52a8\u5b9e\u4f8b\u4e4b\u540e\u5b9a\u65f6\u8bbf\u95eehttp:\/\/192.168.2.100\/somepath\uff0c\u5f53\u6210\u529f\u8bbf\u95ee\u8fd9\u4e2a\u9875\u9762\u4e4b\u540e\uff0c\u4f7f\u7528REST\u63a5\u53e3\u914d\u7f6e\u8be5\u673a\u5668\u7684IP\u5730\u5740\u4e3a\u6240\u9700\u7684\u65b0\u5730\u5740\uff08\u5982200.0.0.2\uff09\uff1b\u8fd9\u4e2a\u65f6\u5019\u7f51\u7edc\u4f1a\u77ed\u6682\u4e0d\u540c\uff0c\u7136\u540e\u5728\u8bbf\u95eehttp:\/\/200.0.0.2\/somepath\uff0c\u5f53\u6210\u529f\u8bbf\u95ee\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u914d\u7f6e\u5404\u79cd\u503c\u3002 \u6574\u4e2a\u8fc7\u7a0b\u6bd4\u8f83\u9ebb\u70e6\uff0c\u6240\u6709\u7684\u914d\u7f6e\u90fd\u9700\u8981\u5b9e\u73b0REST\u63a5\u53e3\uff0c\u65e0\u6cd5\u505a\u5230\u81ea\u5b9a\u4e49\u542f\u52a8\u811a\u672c\u7684\u66f4\u65b0\uff1b\u6700\u4e0d\u53ef\u63a5\u53d7\u7684\u662f\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u4e32\u884c\u7684\uff0c\u5f53\u8981\u542f\u52a8100\u4e2aVM\u65f6\uff0c\u53ea\u80fd\u4e00\u4e2aVM\u4e00\u4e2aVM\u987a\u5e8f\u542f\u52a8\uff0c\u5426\u5219\u4e24\u4e2aVM\u90fd\u6709\u540c\u4e00\u4e2a\u5730\u5740\uff08192.168.2.100\uff09\uff0c\u90a3\u4e48\u7f51\u7edc\u8bbf\u95ee\u5c31\u53ef\u80fd\u51fa\u73b0\u95ee\u9898\u4e86\u3002 \u4e0d\u8fc7\u53d7\u5230\u5404\u79cdStack\u7ba1\u7406\u865a\u62df\u673a\u7528\u5230cloud-init\u7684\u542f\u53d1\uff0c\u6211\u8ba4\u4e3a\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u5957\u5de5\u5177\u5b9e\u73b0\u4e0a\u8ff0\u8fc7\u7a0b\u7684\u3002<\/p>\n<h2>\u4ec0\u4e48\u662fcloud-init<\/h2>\n<p>cloud-init\uff08\u7b80\u79f0ci\uff09\u5728AWS\u3001Openstack\u548cCloudstack\u4e0a\u90fd\u6709\u4f7f\u7528\uff0c\u6240\u4ee5\u5e94\u8be5\u7b97\u662f\u4e8b\u5b9e\u4e0a\u7684\u4e91\u4e3b\u673a\u5143\u6570\u636e\u7ba1\u7406\u6807\u51c6\u3002\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0cgoogle\u76f8\u5173\u7684\u6587\u6863\uff0c\u53d1\u73b0\u4e2d\u6587\u8fd9\u65b9\u9762\u51e0\u4e4e\u6ca1\u6709\uff0cStacker\u4f60\u4eec\u518d\u641e\u867e\u7c73\u5462\uff1f\u5f53\u7136\u8bdd\u8bf4\u56de\u6765\u82f1\u6587\u7684\u8d44\u6599\u9664\u4e86\u5b98\u7f51\u5916\u51e0\u4e4e\u4e5f\u6ca1\u6709\u4ec0\u4e48\uff0c\u6211\u82b1\u4e86\u8fd1\u4e00\u5468\u7684\u65f6\u95f4\u624d\u5f04\u660e\u767d\u4e86\u3002 <\/p>\n<p>\u9996\u5148\u8981\u660e\u786e\u7684\u662fcloud-init\u5728\u5de5\u4f5c\u4e4b\u524d\uff0cVM\u662f\u4eceDHCP\u670d\u52a1\u5668\u83b7\u53d6\u5230\u4e86IP\uff0c\u6240\u6709DHCP\u53d1\u73b0\u4e0d\u662fcloud-init\u7684\u4e8b\u60c5\u3002\u5f53\u4f60\u5728Openstack\u4e2d\u7528ubuntu cloud VM\u542f\u52a8\u5361\u5728cloud-init\u754c\u9762\u65f6\uff0c\u591a\u534a\u662f\u56e0\u4e3aDHCP\u8fd8\u6ca1\u83b7\u53d6IP\uff0c\u800c\u4e0d\u662fcloud-init\u672c\u8eab\u7684\u95ee\u9898\u3002\u90a3\u4e48cloud-init\u4e3b\u8981\u8d70\u4ec0\u4e48\u5462\uff1f\u5b83\u5411\u4e00\u53f0\u6570\u636e\u670d\u52a1\u5668\u83b7\u53d6\u5143\u6570\u636e\uff08meta data\uff09\u548c\u7528\u6237\u6570\u636e\uff08user data\uff09\uff0c\u524d\u8005\u662f\u6307VM\u7684\u5fc5\u8981\u4fe1\u606f\uff0c\u5982\u4e3b\u673a\u540d\u3001\u7f51\u7edc\u5730\u5740\u7b49\uff1b\u540e\u8005\u662f\u7cfb\u7edf\u6216\u7528\u6237\u9700\u8981\u7684\u6570\u636e\u548c\u6587\u4ef6\uff0c\u5982\u7528\u6237\u7ec4\u4fe1\u606f\u3001\u542f\u52a8\u811a\u672c\u7b49\u3002\u5f53cloud-init\u83b7\u53d6\u8fd9\u4e9b\u4fe1\u606f\u540e\uff0c\u5f00\u59cb\u4f7f\u7528\u4e00\u4e9b\u6a21\u5757\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u5982\u65b0\u5efa\u7528\u6237\u3001\u542f\u52a8\u811a\u672c\u7b49\u3002<\/p>\n<h2>cloud-init\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>\u9996\u5148\uff0c\u6570\u636e\u670d\u52a1\u5668\u5f00\u542fHTTP\u670d\u52a1\uff0ccloud-init\u4f1a\u5411\u6570\u636e\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u786e\u8ba4\u6570\u636e\u6e90\u6a21\u5757\uff0c\u4f9d\u6b21\u83b7\u53d6\u7248\u672c\u3001\u6570\u636e\u7c7b\u578b\u548c\u5177\u4f53\u6570\u636e\u5185\u5bb9\u4fe1\u606f\u3002<\/p>\n<h3>\u786e\u8ba4\u6570\u636e\u6e90\u6a21\u5757<\/h3>\n<p>cloud-init\u4f1a\u67e5\u627e\/etc\/cloud\/cloud.cfg.d\/90_dpkg.cfg\u4e2d\u7684datasource_list\u53d8\u91cf\uff0c\u4f9d\u6b21\u4f7f\u7528\u5176\u4e2d\u7684\u6570\u636e\u6e90\u6a21\u5757\uff0c\u9009\u62e9\u4e00\u4e2a\u53ef\u7528\u7684\u6570\u636e\u6e90\u6a21\u5757\u3002\u5982\u6211\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff1adatasource_list: [ Nsfocus, NoCloud, AltCloud, CloudStack, ConfigDrive, Ec2, MAAS, OVF, None ]\uff0c\u90a3\u4e48ci\u9996\u5148\u8c03\u7528$PYTHON_HOME\/dist-packages\/cloudinit\/sources\/DataSourceNsfocus.py\u4e2d\u7c7bDataSourceNsfocus\u7684get_data\u51fd\u6570\uff0c\u5f53\u4e14\u4ec5\u5f53\u8bbf\u95ee\u94fe\u63a5DEF_MD_URL\u4e3a\u6b63\u5e38\u65f6\uff0c\u8fd9\u4e2a\u6570\u636e\u6e90\u88ab\u8ba4\u4e3a\u662fOK\u7684\u3002<\/p>\n<p>\u5728\u6211\u7684\u5b9e\u8df5\u4e2d\uff0cCloudStack\u7684DEF_MD_URL\u4e3aDHCP\u7684\u670d\u52a1\u5668ip\uff0c\u800cOpenstack\u548cAWS\u5219\u4e3a\u4e00\u4e2a\u5e38\u503c169.254.169.254\uff0c\u7136\u540e\u5728\u5bbf\u4e3b\u673a\u7684\u4e2d\u505a\u4e00\u4e2aiptables\u91cd\u5b9a\u5411\uff0c\u8fd9\u6837\u5c31\u5230\u4e86\u6211\u4eec\u7684\u670d\u52a1\u5668\u76d1\u542c\u7aef\u53e38807\uff1a<\/p>\n<pre>\r\n$ sudo ip netns exec ns-router iptables -L -nvx -t nat\r\nChain PREROUTING (policy ACCEPT 169850 packets, 21565088 bytes)\r\n    pkts      bytes target     prot opt in     out     source               destination         \r\n      47     2820 REDIRECT   tcp  --  *      *       0.0.0.0\/0            169.254.169.254      tcp dpt:80 redir ports 8807\r\n$ sudo ip netns exec ns-router iptables -L -nvx\r\nChain INPUT (policy ACCEPT 97027 packets, 8636621 bytes)\r\n    pkts      bytes target     prot opt in     out     source               destination         \r\n       0        0 ACCEPT     tcp  --  *      *       0.0.0.0\/0            127.0.0.1            tcp dpt:8807\r\n<\/pre>\n<h3>\u4e00\u4e9b\u7cfb\u7edf\u5047\u8bbe<\/h3>\n<p>\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0c\u867d\u7136\u6bcf\u4e2a\u6570\u636e\u6e90\u8bbf\u95ee\u7684\u5165\u53e3\u90fd\u662fget_data\uff0c\u4f46\u6bcf\u4e2a\u6570\u636e\u670d\u52a1\u7684\u683c\u5f0f\u548c\u4f4d\u7f6e\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u5143\u6570\u636e\u53ef\u80fd\u5728\/nsfocus\/latest\/metadata\/\uff0c\u4e5f\u53ef\u80fd\u5728\/latest\/metadata.json\uff0c\u4e5f\u5c31\u662f\u8bf4\u6570\u636e\u6e90\u6a21\u5757\u6839\u636e\u81ea\u5df1\u7cfb\u7edf\u7684\u89c4\u5b9a\uff0c\u8bbf\u95ee\u76f8\u5e94\u7684\u6570\u636e\uff0c\u5e76\u6839\u636eci\u7684\u89c4\u5b9a\uff0c\u6307\u5b9a\u5982\u4f55\u5c06\u8fd9\u4e9b\u6570\u636e\u4e0eci\u63a5\u4e0b\u6765\u7684\u5904\u7406\u6a21\u5757\u5bf9\u5e94\u4e0a\u3002<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u7684\u6570\u636e\u8bbf\u95ee\u5730\u5740\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre>\r\n--namespace\r\n           |\r\n           |------version\r\n                        |\r\n                        |---------meta_data.json\r\n                        |---------meta_data\r\n                        |                  |---------public-hostname\r\n                        |                  |---------network_config\r\n                        |\r\n                        |---------user_data\r\n<\/pre>\n<p>\u5176\u4e2d\uff0cnamespace\u4e3ansfocus\uff0cmeta_data.json\u662f\u4e00\u4e2ajson\u6587\u4ef6\uff0c\u91cc\u9762\u5305\u542b\u6240\u6709\u5143\u6570\u636e\u3002<br \/>\n\u5176\u6b21\uff0c\u6211\u4eec\u7684\u6570\u636e\u670d\u52a1\u5668IP\u4e3a111.0.0.2<\/p>\n<h3>\u83b7\u5f97\u5143\u6570\u636e<\/h3>\n<p>\u56e0\u4e3a\u83b7\u53d6\u662fHTTP\u7684\u5f62\u5f0f\uff0c\u6240\u4ee5\u4ee5curl\u4e3a\u4f8b\u8bf4\u660e\u4e0b\u9762\u8fc7\u7a0b\uff1a<\/p>\n<pre>\r\n$ curl http:\/\/111.0.0.2\/nsfocus\r\n1.0\r\nlatest\r\n$ curl http:\/\/111.0.0.2\/nsfocus\/latest\r\nmeta_data\r\nuser_data\r\nmeta_data.json\r\n$ curl http:\/\/111.0.0.2\/nsfocus\/latest\/meta_data\r\npublic-hostname\r\nlocal-ipv4\r\nnetwork_config\r\n...\r\n$ curl http:\/\/111.0.0.2\/nsfocus\/latest\/meta_data\/local-ipv4\r\n111.0.0.11\r\n$ curl http:\/\/111.0.0.2\/nsfocus\/latest\/meta_data.json\r\n{\"files\": {}, \"public_keys\": {\"controller\": \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxtEfzf8I0jA7IHDRHJtDq3nTcTXAWgFYEsAV0i7WU6v8gvFr\/R+DTvkVdFGgbM\/qhVNWUehmPicENac6xldbL5ov6J7c8Y+UytPwJCt13IzDHXaL1BxVYUV6dpe6SYGYohNQ2KZYkG\/95NzjxI1Max5DDvU8mbpEz\/KyphowseburknQTkOTEigJ7CKM4G1eGVhBHKRHXbNsoPZwJnqvIHIpDcwGaj+OgVGF+o3ytH4twrwNwUFiWrUaxo9j2uRTSejYRh1eC9KOYXTnXInzV1xCVHYs\/x+eIzav+2oM8hgR3xr1efgSU2sMzXrp+mJAPzHaAyAat+s7AMDu9tKrd marvel@marvel-ThinkPad-X230\"}, \"hostname\": \"waf-ba-0001\", \"id\": \"waf-ba-0001\", \"network_config\": {\"content_path\": \"latest\/meta_data\/network_config\"}}\r\n<\/pre>\n<p>\u8fd9\u4e2ameta_data.json\u662f\u6211\u4eec\u53c2\u8003Openstack\u7684\u6807\u51c6\uff0c\u81ea\u5df1\u5b9e\u73b0\u7684\u3002\u5f53\u83b7\u5f97meta_data.json\u540e\uff0cDataSourceNsfocus\u89e3\u6790\u91cc\u9762\u7684\u5b57\u6bb5\uff0c\u586b\u5165\u81ea\u5df1\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u5982\u653e\u5165DataSourceNsfocus\u7684result\u5b57\u5178\u4e2d\u3002<\/p>\n<pre>\r\n            if found and translator:\r\n                try:\r\n                    data = translator(data)\r\n                except Exception as e:\r\n                    raise BrokenMetadata(\"Failed to process \"\r\n                                         \"path %s: %s\" % (path, e)) \r\n            if found:\r\n                results[name] = data\r\n<\/pre>\n<p>\u8fd9\u6837\uff0c\u5982hostname\u5c31\u5b58\u4e3aself.result[&#8216;meta&#8217;][&#8216;hostname&#8217;]\u3002<\/p>\n<h3>\u4f9b\u5176\u4ed6\u5904\u7406\u6a21\u5757\u4f7f\u7528\u7684\u83b7\u53d6\u5143\u6570\u636e\u51fd\u6570<\/h3>\n<p>\u5728\u4e0a\u4e00\u9636\u6bb5\uff0c\u5143\u6570\u636e\u7684\u63d0\u4f9b\u3001\u83b7\u53d6\u548c\u5b58\u50a8\u90fd\u662f\u5f88\u81ea\u7531\u7684\uff0c\u90a3\u4e48\u8fd9\u4e9b\u6570\u636e\u600e\u4e48\u88ab\u4f7f\u7528\uff0c\u4f8b\u5982hostname\u600e\u4e48\u8bbe\u7f6e\u5462\uff1f\u90a3\u5c31\u9700\u8981\u6839\u636eci\u7684\u6807\u51c6\u5b9e\u73b0\u4e00\u4e9b\u63a5\u53e3\uff0c\u5982\u8bbe\u7f6ehostname\u5c31\u9700\u8981\u6211\u4eec\u5b9e\u73b0DataSourceNsfocus\u7684get_hostname\u65b9\u6cd5\uff1a<\/p>\n<pre>\r\n    def get_hostname(self, fqdn=False):\r\n        return self.metadata.get(\"hostname\")\r\n<\/pre>\n<p>\u8fd9\u6837\uff0c\u5176\u4ed6\u6a21\u5757\u5982set_hostname\u548cupdate_hostname\u5c31\u4f1a\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u6b63\u786e\u8bbe\u7f6e\u4e3b\u673a\u540d\u4e86\u3002\u5982\u679c\u4f60\u60f3\u8bbe\u7f6e\u5176\u4ed6\u6570\u636e\uff0c\u53ef\u53c2\u8003<a href=\"http:\/\/http:\/\/cloudinit.readthedocs.org\/en\/latest\/topics\/datasources.html\" title=\"cloud-init\u6570\u636e\u6e90\u53c2\u8003\">cloud-init\u6570\u636e\u6e90\u53c2\u8003<\/a>\u7684\u4ecb\u7ecd\u3002\u4e86\u89e3\u8fd8\u6709\u54ea\u4e9b\u5904\u7406\u6a21\u5757\uff0c\u53ef\u8bfb\u4e00\u4e0b\/etc\/cloud\/cloud.cfg\u6587\u4ef6\u3002<\/p>\n<p>\u81f3\u6b64\uff0c\u4e00\u4e9bVM\u6240\u9700\u7684\u5e38\u7528\u914d\u7f6e\u5df2\u7ecf\u641e\u5b9a\uff0c\u90a3\u4e48\u5982\u679c\u6211\u4eec\u60f3\u505a\u4e00\u4e9b\u6d41\u7a0b\u65b9\u9762\u7684\u81ea\u52a8\u4e0b\u53d1\u548c\u8fd0\u884c\u8be5\u600e\u4e48\u505a\u5462\uff1f\u5219\u9700\u8981\u8bbe\u7f6e\u4e00\u4e0buser_data\u3002<\/p>\n<h3>\u83b7\u53d6\u7528\u6237\u6570\u636e<\/h3>\n<p>\u7528\u6237\u6570\u636e\u5305\u62ec\u51e0\u7c7b\uff1a<\/p>\n<ul>\n<li>\u914d\u7f6e\u6587\u4ef6\uff08Cloud Config Data\uff09\uff0c\u7c7b\u578b\u4e3aContent-Type: text\/cloud-config\uff0c\u7cfb\u7edf\u914d\u7f6e\u6587\u4ef6\uff0c\u5982\u7ba1\u7406\u7528\u6237\u7b49\uff0c\u4e0e\/etc\/cloud\u4e0b\u7684cloud.cfg\u6700\u540e\u5408\u5e76\u914d\u7f6e\u9879\uff0c\u66f4\u591a\u7684\u914d\u7f6e\u7ec6\u8282\u53c2\u8003 <a href=\"https:\/\/cloudinit.readthedocs.org\/en\/latest\/topics\/examples.html\">\u914d\u7f6e\u6837\u4f8b<\/a><\/li>\n<li>\u542f\u52a8\u4efb\u52a1\uff08Upstart Job\uff09\uff0c\u7c7b\u578b\u4e3aContent-Type: text\/upstart-job\uff0c\u5efa\u7acbUpstart\u7684\u670d\u52a1<\/li>\n<li>\u7528\u6237\u6570\u636e\u811a\u672c\uff08User-Data Script\uff09\uff0c\u7c7b\u578b\u4e3aContent-Type: text\/x-shellscript\uff0c\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u811a\u672c\uff0c\u5728\u542f\u52a8\u65f6\u6267\u884c<\/li>\n<li>\u5305\u542b\u6587\u4ef6\uff08Include File\uff09\uff0c\u7c7b\u578b\u4e3aContent-Type: text\/x-include-url\uff0c\u8be5\u6587\u4ef6\u5185\u5bb9\u662f\u4e00\u4e2a\u94fe\u63a5\uff0c\u8fd9\u4e2a\u94fe\u63a5\u7684\u5185\u5bb9\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c<\/li>\n<li>\uff08Cloud Boothook\uff09\uff0c\u7c7b\u578b\u4e3aContent-Type: text\/cloud-boothook\uff0c<\/li>\n<li>\u538b\u7f29\u5185\u5bb9\uff08 Gzip Compressed Content\uff09\uff0c<\/li>\n<li>\u5904\u7406\u53e5\u67c4\uff08Part Handler\uff09\uff0c\u7c7b\u578b\u4e3aContent-Type: text\/part-handler\uff0c\u5185\u5bb9\u4e3apython\u811a\u672c\uff0c\u6839\u636e\u7528\u6237\u6570\u636e\u6587\u4ef6\u7684\u7c7b\u578b\u505a\u76f8\u5e94\u7684\u5904\u7406<\/li>\n<li>\u591a\u90e8\u5206\u5b58\u6863\uff08Mime Multi Part archive\uff09\uff0c\u5f53\u5ba2\u6237\u7aef\u9700\u8981\u4e0b\u8f7d\u591a\u4e2a\u4e0a\u8ff0\u7528\u6237\u6570\u636e\u6587\u4ef6\u65f6\uff0c\u53ef\u7528Mime\u7f16\u7801\u4e3aMime Multi Part archive\u4e00\u6b21\u4e0b\u8f7d<\/li>\n<\/ul>\n<h3>\u5b9e\u4f8b<\/h3>\n<p>\u6211\u5728data\u76ee\u5f55\u4e0b\u9762\u5efa\u7acb\u4e09\u4e2a\u6587\u4ef6\uff1a<\/p>\n<p><strong>cloud.config<\/strong><\/p>\n<pre>\r\ngroups:\r\n  - nsfocus: [nsfocus]\r\nusers:\r\n  - default\r\n  - name: nsfocus\r\n    lock-passwd: false \r\n    sudo: ALL=(ALL) NOPASSWD:ALL\r\nsystem_info:\r\n  default_user:\r\n    name: nsfocus\r\n    groups: [nsfocus,sudo]\r\nbootcmd:\r\n  - echo \"#HOSTS\\n127.0.0.1    localhost\\n::1    localhost ip6-localhost\\nff02::1    ip6-allnodes\\nff03::1    ip6-allrouters\\n#ip#    #host#\" > \/etc\/hosts\r\nruncmd:\r\n  - [echo, \"RUNCMD: welcome to nsfocus-------------------------------------------\"]\r\nfinal_message: \"Welcome to NSFOCUS SECURITY #type#=====================================\"\r\n<\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2acloud-config\u6587\u4ef6\uff0c\u5185\u5bb9\u8868\u793a\u65b0\u5efa\u4e00\u4e2ansfocus\u7684\u7528\u6237\uff0c\u5f52\u4e8ensfocus\u548csudo\u7ec4\uff0c\u5728\u542f\u52a8\u65f6\u8fd0\u884cbootcmd\u7684\u547d\u4ee4\u66f4\u65b0hosts\uff0c\u542f\u52a8\u6700\u540e\u8f93\u51fafinal_message\u3002<\/p>\n<p><strong>nsfocus-init.script <\/strong><\/p>\n<pre>\r\n$ cat nsfocus-init.script \r\n#!\/bin\/bash\r\necho \"this is a startup script from nsfocus\" \r\necho \"this is a startup script from nsfocus\" >> \/tmp\/nsfocus-init-script\r\n<\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u811a\u672c\uff0c\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u4f1a\u88ab\u8c03\u7528<\/p>\n<p><strong> nsfocus-init.upstart <\/strong><\/p>\n<pre name=\"code\" class=\"shell\">\r\n$ cat nsfocus-init.upstart \r\ndescription \"a nsfocus upstart job\"\r\n\r\nstart on cloud-config\r\nconsole output\r\ntask\r\nscript\r\necho \"====BEGIN=======\"\r\necho \"HELLO From nsfocus Upstart Job, $UPSTART_JOB\"\r\necho \"HELLO From nsfocus Upstart Job, $UPSTART_JOB\" >> \/tmp\/hello\r\necho \"=====END========\"\r\nend script\r\n<\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\u8bbf\u95ee\uff0c\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u4f1a\u88ab\u542f\u52a8<\/p>\n<p>HTTP\u670d\u52a1\u5668\u6536\u5230\/nsfocus\/latest\/user_data\u65f6\uff0c\u4f5c\u5982\u4e0b\u5904\u7406\uff1a<\/p>\n<pre name=\"code\" class=\"python\">\r\nfrom email.mime.multipart import MIMEMultipart\r\nfrom email.mime.text import MIMEText\r\ndef encode_mime(fps):\r\n    combined_message = MIMEMultipart()\r\n    for fn, patterns in fps:\r\n        print fn\r\n        (filename, format_type) = fn.split(\":\", 1)\r\n        print filename\r\n        print \"---\"\r\n        with open(filename) as fh: \r\n            contents = fh.read()\r\n        for (p, v) in patterns:\r\n            contents = contents.replace(p, v)\r\n    \r\n        sub_message = MIMEText(contents, format_type, sys.getdefaultencoding())\r\n        sub_message.add_header('Content-Disposition', 'attachment; filename=\"%s\"' % (filename[filename.rindex(\"\/\")+1:]))\r\n        combined_message.attach(sub_message)\r\n    return str(combined_message)\r\n\r\n#main process\r\n#....blablabla\r\n      if subtype == \"user_data\":\r\n            if len(arr) == 0:\r\n                res = encode_mime([\r\n                    (\".\/data\/nsfocus-init.upstart:upstart-job\",[]),\r\n                    (\".\/data\/nsfocus-init.script:x-shellscript\",[]),\r\n                    (\".\/data\/cloud.config:cloud-config\",[('#ip#', device.management_ip), ('#host#',device.id), ('#type#', device.type)])])\r\n                return self.gen_resp(200, res)\r\n<\/pre>\n<p>\u865a\u62df\u673a\u542f\u52a8\u4e4b\u540e\uff0c\u670d\u52a1\u5668\u6536\u5230\u8bf7\u6c42\uff0c\u8fd4\u56de\u4e0b\u9762\u7684\u5185\u5bb9\uff1a<\/p>\n<pre>\r\nFrom nobody Fri Dec 26 15:34:36 2014\r\nContent-Type: multipart\/mixed; boundary=\"===============5883341837158849895==\"\r\nMIME-Version: 1.0\r\n\r\n--===============5883341837158849895==\r\nMIME-Version: 1.0\r\nContent-Type: text\/upstart-job; charset=\"us-ascii\"\r\nContent-Transfer-Encoding: 7bit\r\nContent-Disposition: attachment; filename=\"nsfocus-init.upstart\"\r\n\r\ndescription \"a nsfocus upstart job\"\r\n\r\nstart on cloud-config\r\nconsole output\r\ntask\r\nscript\r\necho \"====BEGIN=======\"\r\necho \"HELLO From nsfocus Upstart Job, $UPSTART_JOB\"\r\necho \"HELLO From nsfocus Upstart Job, $UPSTART_JOB\" >> \/tmp\/hello\r\necho \"=====END========\"\r\nend script\r\n\r\n--===============5883341837158849895==\r\nMIME-Version: 1.0\r\nContent-Type: text\/x-shellscript; charset=\"us-ascii\"\r\nContent-Transfer-Encoding: 7bit\r\nContent-Disposition: attachment; filename=\"nsfocus-init.script\"\r\n\r\n#!\/bin\/bash\r\necho \"this is a startup script from nsfocus\" \r\necho \"this is a startup script from nsfocus\" >> \/tmp\/nsfocus-init-script\r\n\r\n--===============5883341837158849895==\r\nMIME-Version: 1.0\r\nContent-Type: text\/cloud-config; charset=\"us-ascii\"\r\nContent-Transfer-Encoding: 7bit\r\nContent-Disposition: attachment; filename=\"cloud.config\"\r\n\r\ngroups:\r\n  - nsfocus: [nsfocus]\r\n  - dev\r\nusers:\r\n  - default\r\n  - name: nsfocus\r\n    lock-passwd: false \r\n    sudo: ALL=(ALL) NOPASSWD:ALL\r\nsystem_info:\r\n  default_user:\r\n    name: nsfocus\r\n    groups: [nsfocus,sudo]\r\nbootcmd:\r\n  - echo \"#HOSTS\\n127.0.0.1    localhost\\n::1    localhost ip6-localhost\\nff02::1    ip6-allnodes\\nff03::1    ip6-allrouters\\n111.0.0.12    waf-ba-0001\" > \/etc\/hosts\r\nruncmd:\r\n  - [echo, \"RUNCMD: welcome to nsfocus-------------------------------------------\"]\r\n\r\nfinal_message: \"Welcome to NSFOCUS SECURITY waf=====================================\"\r\n\r\n--===============5883341837158849895==--\r\n<\/pre>\n<p>VM\u542f\u52a8\u754c\u9762\u6253\u5370\u5982\u4e0b\u4fe1\u606f\uff0c\u4e14\u4e3b\u673a\u540d\u53d8\u6210\u4e86\u6211\u4eec\u9884\u5b9a\u7684\u503c\uff0c\u8bf4\u660e\u786e\u5b9e\u83b7\u53d6meta-data\u548cuser-data\u6210\u529f\uff0c\u811a\u672c\u8fd0\u884c\u4e5f\u6210\u529f\u4e86\u3002\u4e0d\u8fc7\u8981\u8bf4\u660e\u4e00\u70b9\uff0cupstart\u5728Ubuntu\u4e0a\u6ca1\u95ee\u9898\uff0c\u4f46Debian\u6ca1\u901a\u8fc7\uff0c\u53ef\u80fd\u5f53\u524d\u9636\u6bb5Debian\u7684\u542f\u52a8\u673a\u5236\u8fd8\u6709\u4e00\u4e9b\u533a\u522b\uff0c\u6240\u4ee5\u8fd8\u662f\u4f7f\u7528bootcmd\u6216\u542f\u52a8\u811a\u672c\u7684\u65b9\u5f0f\u542f\u52a8\u3002<\/p>\n<p><a href=\"https:\/\/marvelliu.space\/wp-content\/uploads\/2014\/12\/login.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-577\" src=\"https:\/\/marvelliu.space\/wp-content\/uploads\/2014\/12\/login-300x232.png\" alt=\"Ubuntu\u767b\u9646\u9875\u9762\" width=\"500\" height=\"432\" \/><\/a><\/p>\n<h5>\u53c2\u8003\u6587\u732e<\/h5>\n<p>cloud-init\u6570\u636e\u6e90\u53c2\u8003 http:\/\/cloudinit.readthedocs.org\/en\/latest\/topics\/datasources.html<br \/>\ndnsmasq\u53c2\u8003 http:\/\/www.thekelleys.org.uk\/dnsmasq\/docs\/dnsmasq-man.html<br \/>\n\u66f4\u591a\u6837\u4f8b https:\/\/github.com\/number5\/cloud-init\/blob\/master\/doc\/examples\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48\u8981\u7528cloud-init \u4e0d\u540c\u79cd\u7c7b\u7684\u8bbe\u5907VM\u542f\u52a8\u603b\u662f\u4e00\u4ef6\u975e\u5e38\u9ebb\u70e6\u7684\u4e8b\u60c5\uff0c\u4f8b\u5982\u5b89\u5168\u8bbe\u5907\u6709WAF\u3001IPS\u7b49\uff0c\u6bcf\u79cd\u8bbe\u5907\u7684\u7f51\u7edc\u63a5\u53e3\u3001\u542f\u52a8\u811a\u672c\u4e92\u4e0d\u4e00\u6837\uff0c\u5373\u4fbf\u540c\u4e00\u79cd\u8bbe\u5907\uff0c\u5176\u4e3b\u673a\u540d\u3001\u7f51\u7edc\u5730\u5740\u7b49\u4e5f\u4e0d\u4e00\u6837\u3002\u90a3\u4e48\u5982\u4f55\u5bf9\u8fd9\u4e9bVM\u542f\u52a8\u8fc7\u7a0b\u8fdb\u884c\u7ba1\u7406\uff0c\u5e76\u5b8c\u6210\u6240\u6709\u6570\u636e\u7684\u914d\u7f6e\u5462\uff1f \u5728\u8fd9\u4e4b\u524d\uff0c\u6211\u7684\u5b9e\u4e60\u751f\u662f\u600e\u4e48\u505a\u7684\uff1a\u5c06\u4e00\u53f0VM\u7684\u7ba1\u7406\u53e3\u7f51\u7edc\u5730\u5740\u8bbe\u7f6e\u4e3a192.168.2.100\uff0c\u7136\u540e\u6bcf\u6b21\u542f\u52a8\u5b9e\u4f8b\u4e4b\u540e\u5b9a\u65f6\u8bbf\u95eehttp:\/\/192.168.2.100\/somepath\uff0c\u5f53\u6210\u529f\u8bbf\u95ee\u8fd9\u4e2a\u9875\u9762\u4e4b\u540e\uff0c\u4f7f\u7528REST\u63a5\u53e3\u914d\u7f6e\u8be5\u673a\u5668\u7684IP\u5730\u5740\u4e3a\u6240\u9700\u7684\u65b0\u5730\u5740\uff08\u5982200.0.0.2\uff09\uff1b\u8fd9\u4e2a\u65f6\u5019\u7f51\u7edc\u4f1a\u77ed\u6682\u4e0d\u540c\uff0c\u7136\u540e\u5728\u8bbf\u95eehttp:\/\/200.0.0.2\/somepath\uff0c\u5f53\u6210\u529f\u8bbf\u95ee\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u914d\u7f6e\u5404\u79cd\u503c\u3002 \u6574\u4e2a\u8fc7\u7a0b\u6bd4\u8f83\u9ebb\u70e6\uff0c\u6240\u6709\u7684\u914d\u7f6e\u90fd\u9700\u8981\u5b9e\u73b0REST\u63a5\u53e3\uff0c\u65e0\u6cd5\u505a\u5230\u81ea\u5b9a\u4e49\u542f\u52a8\u811a\u672c\u7684\u66f4\u65b0\uff1b\u6700\u4e0d\u53ef\u63a5\u53d7\u7684\u662f\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u4e32\u884c\u7684\uff0c\u5f53\u8981\u542f\u52a8100\u4e2aVM\u65f6\uff0c\u53ea\u80fd\u4e00\u4e2aVM\u4e00\u4e2aVM\u987a\u5e8f\u542f\u52a8\uff0c\u5426\u5219\u4e24\u4e2aVM\u90fd\u6709\u540c\u4e00\u4e2a\u5730\u5740\uff08192.168.2.100\uff09\uff0c\u90a3\u4e48\u7f51\u7edc\u8bbf\u95ee\u5c31\u53ef\u80fd\u51fa\u73b0\u95ee\u9898\u4e86\u3002 \u4e0d\u8fc7\u53d7\u5230\u5404\u79cdStack\u7ba1\u7406\u865a\u62df\u673a\u7528\u5230cloud-init\u7684\u542f\u53d1\uff0c\u6211\u8ba4\u4e3a\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u5957\u5de5\u5177\u5b9e\u73b0\u4e0a\u8ff0\u8fc7\u7a0b\u7684\u3002 \u4ec0\u4e48\u662fcloud-init cloud-init\uff08\u7b80\u79f0ci\uff09\u5728AWS\u3001Openstack\u548cCloudstack\u4e0a\u90fd\u6709\u4f7f\u7528\uff0c\u6240\u4ee5\u5e94\u8be5\u7b97\u662f\u4e8b\u5b9e\u4e0a\u7684\u4e91\u4e3b\u673a\u5143\u6570\u636e\u7ba1\u7406\u6807\u51c6\u3002\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0cgoogle\u76f8\u5173\u7684\u6587\u6863\uff0c\u53d1\u73b0\u4e2d\u6587\u8fd9\u65b9\u9762\u51e0\u4e4e\u6ca1\u6709\uff0cStacker\u4f60\u4eec\u518d\u641e\u867e\u7c73\u5462\uff1f\u5f53\u7136\u8bdd\u8bf4\u56de\u6765\u82f1\u6587\u7684\u8d44\u6599\u9664\u4e86\u5b98\u7f51\u5916\u51e0\u4e4e\u4e5f\u6ca1\u6709\u4ec0\u4e48\uff0c\u6211\u82b1\u4e86\u8fd1\u4e00\u5468\u7684\u65f6\u95f4\u624d\u5f04\u660e\u767d\u4e86\u3002 \u9996\u5148\u8981\u660e\u786e\u7684\u662fcloud-init\u5728\u5de5\u4f5c\u4e4b\u524d\uff0cVM\u662f\u4eceDHCP\u670d\u52a1\u5668\u83b7\u53d6\u5230\u4e86IP\uff0c\u6240\u6709DHCP\u53d1\u73b0\u4e0d\u662fcloud-init\u7684\u4e8b\u60c5\u3002\u5f53\u4f60\u5728Openstack\u4e2d\u7528ubuntu cloud VM\u542f\u52a8\u5361\u5728cloud-init\u754c\u9762\u65f6\uff0c\u591a\u534a\u662f\u56e0\u4e3aDHCP\u8fd8\u6ca1\u83b7\u53d6IP\uff0c\u800c\u4e0d\u662fcloud-init\u672c\u8eab\u7684\u95ee\u9898\u3002\u90a3\u4e48cloud-init\u4e3b\u8981\u8d70\u4ec0\u4e48\u5462\uff1f\u5b83\u5411\u4e00\u53f0\u6570\u636e\u670d\u52a1\u5668\u83b7\u53d6\u5143\u6570\u636e\uff08meta data\uff09\u548c\u7528\u6237\u6570\u636e\uff08user data\uff09\uff0c\u524d\u8005\u662f\u6307VM\u7684\u5fc5\u8981\u4fe1\u606f\uff0c\u5982\u4e3b\u673a\u540d\u3001\u7f51\u7edc\u5730\u5740\u7b49\uff1b\u540e\u8005\u662f\u7cfb\u7edf\u6216\u7528\u6237\u9700\u8981\u7684\u6570\u636e\u548c\u6587\u4ef6\uff0c\u5982\u7528\u6237\u7ec4\u4fe1\u606f\u3001\u542f\u52a8\u811a\u672c\u7b49\u3002\u5f53cloud-init\u83b7\u53d6\u8fd9\u4e9b\u4fe1\u606f\u540e\uff0c\u5f00\u59cb\u4f7f\u7528\u4e00\u4e9b\u6a21\u5757\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u5982\u65b0\u5efa\u7528\u6237\u3001\u542f\u52a8\u811a\u672c\u7b49\u3002 cloud-init\u5de5\u4f5c\u539f\u7406 \u9996\u5148\uff0c\u6570\u636e\u670d\u52a1\u5668\u5f00\u542fHTTP\u670d\u52a1\uff0ccloud-init\u4f1a\u5411\u6570\u636e\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u786e\u8ba4\u6570\u636e\u6e90\u6a21\u5757\uff0c\u4f9d\u6b21\u83b7\u53d6\u7248\u672c\u3001\u6570\u636e\u7c7b\u578b\u548c\u5177\u4f53\u6570\u636e\u5185\u5bb9\u4fe1\u606f\u3002 \u786e\u8ba4\u6570\u636e\u6e90\u6a21\u5757 cloud-init\u4f1a\u67e5\u627e\/etc\/cloud\/cloud.cfg.d\/90_dpkg.cfg\u4e2d\u7684datasource_list\u53d8\u91cf\uff0c\u4f9d\u6b21\u4f7f\u7528\u5176\u4e2d\u7684\u6570\u636e\u6e90\u6a21\u5757\uff0c\u9009\u62e9\u4e00\u4e2a\u53ef\u7528\u7684\u6570\u636e\u6e90\u6a21\u5757\u3002\u5982\u6211\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff1adatasource_list: [ Nsfocus, NoCloud, AltCloud, CloudStack, ConfigDrive, Ec2, MAAS, OVF, None ]\uff0c\u90a3\u4e48ci\u9996\u5148\u8c03\u7528$PYTHON_HOME\/dist-packages\/cloudinit\/sources\/DataSourceNsfocus.py\u4e2d\u7c7bDataSourceNsfocus\u7684get_data\u51fd\u6570\uff0c\u5f53\u4e14\u4ec5\u5f53\u8bbf\u95ee\u94fe\u63a5DEF_MD_URL\u4e3a\u6b63\u5e38\u65f6\uff0c\u8fd9\u4e2a\u6570\u636e\u6e90\u88ab\u8ba4\u4e3a\u662fOK\u7684\u3002 \u5728\u6211\u7684\u5b9e\u8df5\u4e2d\uff0cCloudStack\u7684DEF_MD_URL\u4e3aDHCP\u7684\u670d\u52a1\u5668ip\uff0c\u800cOpenstack\u548cAWS\u5219\u4e3a\u4e00\u4e2a\u5e38\u503c169.254.169.254\uff0c\u7136\u540e\u5728\u5bbf\u4e3b\u673a\u7684\u4e2d\u505a\u4e00\u4e2aiptables\u91cd\u5b9a\u5411\uff0c\u8fd9\u6837\u5c31\u5230\u4e86\u6211\u4eec\u7684\u670d\u52a1\u5668\u76d1\u542c\u7aef\u53e38807\uff1a $ sudo ip netns exec ns-router iptables -L -nvx -t nat Chain PREROUTING (policy ACCEPT 169850 packets, 21565088 bytes) pkts bytes target prot opt in out source &hellip; <a href=\"https:\/\/marvelliu.space\/?p=575\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;\u4f7f\u7528cloud-init\u5b9e\u73b0\u865a\u62df\u673a\u4fe1\u606f\u7ba1\u7406&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-575","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts\/575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=575"}],"version-history":[{"count":15,"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts\/575\/revisions"}],"predecessor-version":[{"id":592,"href":"https:\/\/marvelliu.space\/index.php?rest_route=\/wp\/v2\/posts\/575\/revisions\/592"}],"wp:attachment":[{"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/marvelliu.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}