<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<title type="text">Oh My Stack</title>
<subtitle type="text">JiangJun</subtitle>
<generator uri="https://github.com/mojombo/jekyll">Jekyll</generator>
<link rel="self" type="application/atom+xml" href="http://ohmystack.com/feed.xml" />
<link rel="alternate" type="text/html" href="http://ohmystack.com" />
<updated>2021-11-15T03:49:21+08:00</updated>
<id>http://ohmystack.com/</id>
<author>
  <name>JunJiang江骏</name>
  <uri>http://ohmystack.com/</uri>
  <email>jiangjun1990@gmail.com</email>
</author>


<entry>
  <title type="html"><![CDATA[机器学习的数据隐私(2/3)-联邦学习]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/data-privacy-for-machine-learning-2-federated-learning" />
  <id>http://ohmystack.com/articles/data-privacy-for-machine-learning-2-federated-learning</id>
  <published>2021-11-01T00:00:00+08:00</published>
  <updated>2021-11-01T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;联邦学习（federated learning，FL）是一种协议，其中机器学习模型的训练分布在许多不同的设备上，并且训练后的模型在中心服务器上进行合并。关键点是原始数据永远不会离开单独的设备，也永远不会被集中在一个地方。这与在一个集中的地方收集数据集然后训练模型的传统架构大不相同。&lt;/p&gt;

&lt;p&gt;FL通常在拥有分布式数据的手机或用户浏览器环境中很有用。另一个潜在的使用场景是共享分布在多个数据所有者之间的敏感数据。例如，一个AI初创公司想训练模型以检测皮肤癌。皮肤癌的图片归许多医院所有，但由于隐私和法律方面的考虑，无法将它们集中在一个地方。FL能够使初创企业在数据不离开医院的情况下训练模型。&lt;/p&gt;

&lt;p&gt;在FL设置中，每个客户端都会收到模型架构和一些训练指令。我们会在每个客户端的设备上训练模型，并将权重返回到中央服务器。这会稍微增加隐私性，因为相对于原始数据，一个拦截器更难从模型权重中了解有关用户的任何信息，但它不能提供任何隐私性的保证。分布式模型训练这一步并不能在公司收集数据过程中为用户提升更多隐私性，因为公司通常可以在了解模型架构和权重的情况下得出原始数据原本是什么样的。&lt;/p&gt;

&lt;p&gt;不过，使用FL提高隐私性还有一个很重要的步骤：将权重安全地聚合到中央模型中。有很多算法可以做到这一点，但是它们都要求中央模型必须是可信的，不会试图在合并权重之前对其进行查看。&lt;/p&gt;

&lt;p&gt;图1展示了在FL设置中哪些参与方可以访问用户的个人数据。公司可能会收集数据来设置安全聚合，这样它们就不会看到用户返回的模型权重了。中立的第三方也可以执行安全聚合。在这种情况下，只有用户才能看到他们的数据。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2021-10-15-data-privacy-2-1.png&quot; alt=&quot;recording&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;图1 联邦学习中的受信方&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;FL的另一个隐私保护扩展是将DP合并到此技术中。在这种情况下，DP会限制每个用户可以为最终模型提供信息的数量。研究表明，如果用户数量很大，那么生成的模型几乎与无DP模型一样准确。不过，到目前为止，其尚未在TensorFlow或PyTorch中实现。&lt;/p&gt;

&lt;p&gt;一个生产环境中的FL案例是Android手机的Google Gboard键盘。谷歌可以训练模型以做出更好的下一词预测，而无须了解用户的隐私消息。FL在具有以下特征的场景中最有用：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;模型所需的数据只能从分布式的来源收集；&lt;/li&gt;
  &lt;li&gt;数据源的数量很大；&lt;/li&gt;
  &lt;li&gt;数据在某种程度上是敏感的；&lt;/li&gt;
  &lt;li&gt;数据不需要额外的打标（标签可以由用户直接提供，不会离开源）；&lt;/li&gt;
  &lt;li&gt;理想情况下，数据是从几乎相同的分布中提取的。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;FL在机器学习系统的设计中引入了许多新的考虑因素，例如，并非所有数据源都在一次训练和下一次训练之间收集了新数据，并非所有移动设备都始终处于开启状态，以此类推……所收集的数据通常是不平衡的，并且实际上对于每个设备都是唯一的。当设备池很大时，最容易为每次训练获得足够的数据。必须为任何使用了FL的项目开发新的安全基础设施。&lt;/p&gt;

&lt;p&gt;必须注意避免在训练FL模型的设备上出现性能问题。训练会迅速耗尽移动设备上的电量或导致大量的流量被使用，从而给用户带来费用。尽管移动电话的处理能力正在迅速增强，但它们仍然只能训练小型模型，因此应在中央服务器上训练更复杂的模型。&lt;/p&gt;

&lt;h2 id=&quot;tensorflow中的联邦学习&quot;&gt;TensorFlow中的联邦学习&lt;/h2&gt;

&lt;p&gt;TensorFlow Federated（TFF）会模拟FL的分布式设置，并包含一个可以计算分布式数据更新的随机梯度下降（stochastic gradient descent，SGD）版本。常规SGD要求梯度更新是对中心化数据集的batch计算出来的，然而在联邦设置中不存在这样的中心化数据集。目前，TFF主要针对新的联邦算法进行研究和实验。&lt;/p&gt;

&lt;p&gt;PySyft是由OpenMined组织开发的开源Python平台，用于隐私保护机器学习。它包含使用安全的多方计算来聚合数据的FL实现。它最初是为支持PyTorch模型而开发的，但已经发布了TensorFlow版本。&lt;/p&gt;

&lt;hr /&gt;

&lt;blockquote&gt;
  &lt;p&gt;本文节选自我参与翻译的《机器学习流水线实战》，并做了重新编辑。
阅读更多内容，可以购买实体书：&lt;a href=&quot;https://item.jd.com/10039673039939.html&quot;&gt;https://item.jd.com/10039673039939.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/data-privacy-for-machine-learning-2-federated-learning&quot;&gt;机器学习的数据隐私(2/3)-联邦学习&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on November 01, 2021.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[机器学习的数据隐私(1/3)-差分隐私]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/data-privacy-for-machine-learning-1-differential-privacy" />
  <id>http://ohmystack.com/articles/data-privacy-for-machine-learning-1-differential-privacy</id>
  <published>2021-10-15T00:00:00+08:00</published>
  <updated>2021-10-15T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;隐私保护机器学习是一个非常活跃的研究领域。这里将解释最有技术背后的一些原理并展示一些实用的案例，来说明如何将它们适配进机器学习流水线。&lt;/p&gt;

&lt;p&gt;这次将通过几篇文章，分别介绍3种用于隐私保护机器学习的主要方法：差分隐私、联邦学习和加密的机器学习。&lt;/p&gt;

&lt;h1 id=&quot;数据隐私问题&quot;&gt;数据隐私问题&lt;/h1&gt;

&lt;p&gt;数据隐私是关于信任和限制曝光人们原本希望保密的数据内容。隐私保护机器学习有很多不同的方法，为了在它们之间进行选择，你应该尝试回答以下问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;你试图向谁保密？&lt;/li&gt;
  &lt;li&gt;系统的哪些部分可以是私有的，哪些可以公开？&lt;/li&gt;
  &lt;li&gt;谁是可以查看数据的可信方？&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这些问题的答案将帮助你确定哪种方法最适合你的用例。&lt;/p&gt;

&lt;h2 id=&quot;为什么关心数据隐私&quot;&gt;为什么关心数据隐私&lt;/h2&gt;

&lt;p&gt;数据隐私正在成为机器学习项目的重要组成部分。围绕用户隐私有许多法律要求，比如，于2018年5月生效的欧盟的《通用数据保护条例》（GDPR）和于2020年1月生效的《加利福尼亚州消费者隐私法案》。把个人信息用作机器学习的数据是有道德问题的，那些用着由机器学习提供能力的产品的用户们开始深切关注他们的数据后期会发生什么。由于机器学习一贯依赖数据，并且机器学习模型做出的许多预测是基于从用户那里收集的个人数据，因此机器学习处于数据隐私纷争的最前沿。&lt;/p&gt;

&lt;p&gt;目前，涉及隐私是要付出代价的：加入隐私会带来模型准确率、计算时间或两者均涉及的代价。在一种极端情况下，不收集任何数据会使交互完全保密，但对机器学习一点儿用处也没有。在另一种极端情况下，知道一个人的所有细节可能会损害这个人的隐私，但是这使我们能够建立非常准确的机器学习模型。我们已经开始设想隐私保护机器学习的开发，其可以在不对模型准确率进行较大妥协的情况下加入隐私。&lt;/p&gt;

&lt;p&gt;在某些场景中，隐私保护机器学习可以帮助你使用由于隐私问题而无法用于训练机器学习模型的数据。然而，它并不是仅因为你使用了一种方法即可让你自由地掌控数据。你应该与其他干系人（比如数据所有者、隐私权专家，甚至你公司的法律团队）讨论你的计划。&lt;/p&gt;

&lt;h2 id=&quot;最简单的增加隐私的方法&quot;&gt;最简单的增加隐私的方法&lt;/h2&gt;

&lt;p&gt;通常，构建由机器学习提供支持的产品的默认策略是收集所有可能的数据，然后再决定哪些对训练机器学习模型有用。尽管这是在用户同意的情况下完成的，但增加用户隐私的最简单方法是仅收集训练特定模型所需的数据。对于结构化数据，可以简单地删除姓名、性别或种族等字段。文本或图像数据可以被处理以删除许多个人信息，比如从图像中删除面孔或从文本中删除姓名。但是，在某些情况下，这可能会降低数据的实用性或使其无法训练准确的模型。而且，如果没有收集有关种族和性别的数据，就无法判断模型是否对特定群体有偏见。&lt;/p&gt;

&lt;p&gt;对收集哪些数据的控制权也可以交给用户：与简单地选择加入或退出相比，收集数据的授权可能会更加细致，产品的用户可以确切指定能够收集关于他们的哪些数据。这就带来了设计挑战：提供较少数据的用户是否比提供更多数据的用户获得了更少的准确预测？如何通过机器学习流水线跟踪授权？如何衡量隐私对模型中单个特征的影响？这些都是机器学习社区中需要进行更多讨论的问题。&lt;/p&gt;

&lt;h2 id=&quot;哪些数据需要保密&quot;&gt;哪些数据需要保密&lt;/h2&gt;

&lt;p&gt;在机器学习流水线中，数据通常是从人们那里收集的，但是某些数据对隐私保护机器学习有更高的要求。个人识别信息（personally identifyinginformation，PII）是可以直接识别一个人的数据，比如他们的姓名、电子邮件地址、街道地址、ID号等，因此需要保密。PII会出现在自由文本里，比如反馈评论或客户服务数据，而不仅仅是在直接要求用户提供此数据时。在某些情况下，人像也可以被视为PII。围绕这方面通常有法律标准，如果你的公司有隐私权团队，最好在着手使用此类数据的项目之前咨询他们。&lt;/p&gt;

&lt;p&gt;敏感数据也需要特别注意，通常将它定义为如果发布可能会对某人造成伤害的数据，比如健康数据或专有的公司数据（比如财务数据）。应注意确保在机器学习模型的预测中不会泄露此类数据。&lt;/p&gt;

&lt;p&gt;另一类是“准标识”数据。如果已知足够多的准标识符，比如位置跟踪或信用卡交易数据，则准标识符可以唯一地标识某人。如果已知有关同一个人的多个位置点，则这将提供一种可以与其他数据集组合的唯一追踪来重新识别这个人。2019年12月，《纽约时报》发表了一篇深入介绍使用手机数据进行重新标识的文章（&lt;em&gt;Twelve Million Phones, One Dataset, Zero Privacy&lt;/em&gt;），这还只代表了几种质疑此类数据发布的意见里的一种。&lt;/p&gt;

&lt;h1 id=&quot;差分隐私&quot;&gt;差分隐私&lt;/h1&gt;

&lt;p&gt;如果在机器学习流水线中发现需要额外的隐私，那么可以使用多种方法来帮助增加隐私性，同时保留尽可能多的数据实用性。我们将讨论的第一个问题是&lt;strong&gt;差分隐私&lt;/strong&gt;（differential privacy，DP）。“差分隐私”源自Cynthia Dwork的文章“Differential Privacy”，这篇文章被收录在《密码学与安全百科全书》一书中。差分隐私是这样一种想法的形式化：一个查询或数据集的转换不应该揭示某个人是否在该数据集中。它定义了一个人被加入数据集中所遭受的隐私损失这样一个数学度量，并通过添加噪声将隐私损失降至最低。&lt;/p&gt;

&lt;p&gt;换句话说，如果将一个人从该数据集中删除，则尊重隐私的数据集的变换不应发生变化。在机器学习模型的场景下，如果在考虑隐私的情况下对模型进行了训练，那么如果从训练集中删除一个人，则模型所做的预测不应更改。DP通过在转换中添加某种形式的噪声或随机性来实现。&lt;/p&gt;

&lt;p&gt;举一个更具体的例子，实现差分隐私的一个最简单的方法就是随机回复的概念，如图1所示。 这在提出敏感问题的调查中很有用，比如“你曾被判为犯罪吗？”为了回答这个问题，被问到的人会抛硬币。如果出现正面，他们就如实回答。如果出现背面，则再抛一次硬币。如果硬币是正面，回答“是”；如果硬币是背面，回答“否”。这给了他们推脱之辞：他们可以说给出的是随机答案，而不是真实的答案。由于我们知道抛硬币的概率，因此如果问很多人这个问题，就可以以合理的准确率来计算被判为犯罪的人数比例。当更多的人参加调查时，计算的准确率将提高。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2021-10-15-data-privacy-1-1.png&quot; alt=&quot;recording&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;图1 随机回复流程图&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;这些随机转换是DP的关键。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;假设每人一个训练样本&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;为简单起见，本文会假设数据集中的每个训练样本都与一个人相关或是从一个人那里收集到的。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;局部差分隐私和全局差分隐私&quot;&gt;局部差分隐私和全局差分隐私&lt;/h2&gt;

&lt;p&gt;DP可以分为两种主要方法：局部DP和全局DP。在局部DP中，如先前的随机回复案例所示，在个人级别添加了噪声或随机性，因此在个人与数据采集者之间保持了隐私。在全局DP中，噪声会被添加到整个数据集的转换中。数据采集者与原始数据是可以信任的，但是转换结果不会显示有关个人的数据。&lt;/p&gt;

&lt;p&gt;与局部DP相比，全局DP只要求添加更少的噪声，这可以提高查询的实用性或准确率，从而获得相似的隐私保证。不利之处在于，数据采集者必须被全局DP信任，而对于局部DP，只有一个个用户看得到他们自己的原始数据。&lt;/p&gt;

&lt;h2 id=&quot;epsilondelta和隐私预算&quot;&gt;Epsilon、Delta和隐私预算&lt;/h2&gt;

&lt;p&gt;实现DP的最常见方法可能是用&lt;em&gt;ε&lt;/em&gt;–&lt;em&gt;δ&lt;/em&gt;(epsilon–delta)DP&lt;em&gt;ε&lt;/em&gt;。当比较包含某一特定人的数据集上的随机转换结果与不包含该人的另一结果时，e&lt;strong&gt;&lt;em&gt;ε&lt;/em&gt;&lt;/strong&gt;描述了这些转换结果之间的最大差异。因此，如果&lt;em&gt;ε&lt;/em&gt;为0，说明两个转换会返回完全相同的结果。如果&lt;em&gt;ε&lt;/em&gt;的值较小，则转换将返回相同结果的可能性更大——&lt;em&gt;ε&lt;/em&gt;的值越小越隐私，因为&lt;em&gt;ε&lt;/em&gt;会衡量隐私保证的强度。如果多次查询数据集，则需要对每个查询的&lt;em&gt;ε&lt;/em&gt;求和，以获取总的隐私预算。&lt;/p&gt;

&lt;p&gt;&lt;em&gt;δ&lt;/em&gt;是&lt;em&gt;ε&lt;/em&gt;不成立的概率，或个体数据在随机变换结果中被暴露的概率。通常将&lt;em&gt;δ&lt;/em&gt;设置为人数规模的倒数：对于包含2000人的数据集，将&lt;em&gt;δ&lt;/em&gt;设置为1/1000。有关其背后的数学运算的更多细节，请参见Dwork和Roth合著的&lt;em&gt;The Algorithmic Foundations of Differential Privacy&lt;/em&gt;。&lt;/p&gt;

&lt;p&gt;应该选择什么值作为&lt;em&gt;ε&lt;/em&gt;呢？&lt;em&gt;ε&lt;/em&gt;使我们能够比较不同算法和方法的隐私，但是赋予我们“足够”隐私的绝对值取决于使用场景。&lt;/p&gt;

&lt;p&gt;为了确定要用于&lt;em&gt;ε&lt;/em&gt;的值，当&lt;em&gt;ε&lt;/em&gt;减小时，对系统的准确率可能会有所帮助。选择最私密的参数，同时保留对业务问题可接受的数据实用性。或者，如果泄露数据的后果非常严重，则你可能希望先设置可接受的&lt;em&gt;ε&lt;/em&gt;和&lt;em&gt;δ&lt;/em&gt;值，然后调整其他超参数以获得可能的最佳模型准确率。 &lt;em&gt;ε&lt;/em&gt;–&lt;em&gt;δ&lt;/em&gt; DP的一个弱点是&lt;em&gt;ε&lt;/em&gt;不容易解释。我们正在开发其他方法来帮助解决此问题，比如在模型的训练数据中植入秘密，并衡量在模型预测中暴露这些秘密的可能性。&lt;/p&gt;

&lt;h2 id=&quot;机器学习的差分隐私&quot;&gt;机器学习的差分隐私&lt;/h2&gt;

&lt;p&gt;如果希望将DP用作机器学习流水线的一部分，有一些当前可行的选项能加入DP，尽管我们希望将来会看到更多。第一种选项，DP可以包含在联邦学习系统中，并且可以使用全局DP或局部DP。第二种选项，TensorFlow Privacy库是全局DP的一个例子：原始数据可用于模型训练。&lt;/p&gt;

&lt;p&gt;第三种选项是“教师模型全体的隐私聚合”（private aggregation of teacher ensemble，PATE）方法。这是一种数据共享方案：假设有10个人有标记好的数据，但你没有，他们会在局部训练模型并各自对你的数据做出预测。然后执行DP查询来针对数据集中的每个样本生成最终预测，这样你便不知道10个模型中哪个做了预测。然后，根据这些预测训练出一个新模型，该模型通过一种无法了解这些隐藏数据集的方法囊括了来自10个隐藏数据集的信息。PATE框架展示了在这种场景下如何使用&lt;em&gt;ε&lt;/em&gt;。&lt;/p&gt;

&lt;hr /&gt;

&lt;blockquote&gt;
  &lt;p&gt;本文节选自我参与翻译的《机器学习流水线实战》，并做了重新编辑。
阅读更多内容，可以购买实体书：&lt;a href=&quot;https://item.jd.com/10039673039939.html&quot;&gt;https://item.jd.com/10039673039939.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/data-privacy-for-machine-learning-1-differential-privacy&quot;&gt;机器学习的数据隐私(1/3)-差分隐私&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on October 15, 2021.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[GDG Shanghai - 2017 TensorFlow Summit Recap]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/tensorflow-summit-recap" />
  <id>http://ohmystack.com/articles/tensorflow-summit-recap</id>
  <published>2017-02-18T00:00:00+08:00</published>
  <updated>2017-02-18T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;h2 id=&quot;slideshare&quot;&gt;Slideshare&lt;/h2&gt;

&lt;iframe src=&quot;//www.slideshare.net/slideshow/embed_code/key/2mCYZg52eGGjan&quot; width=&quot;595&quot; height=&quot;485&quot; frameborder=&quot;0&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; scrolling=&quot;no&quot; style=&quot;border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;&quot; allowfullscreen=&quot;&quot;&gt; &lt;/iframe&gt;
&lt;div style=&quot;margin-bottom:5px&quot;&gt; &lt;strong&gt; &lt;a href=&quot;//www.slideshare.net/jiangjun1990/gdgshanghai-2017-tensorflow-summit-recap&quot; title=&quot;GDG-Shanghai 2017 TensorFlow Summit Recap&quot; target=&quot;_blank&quot;&gt;GDG-Shanghai 2017 TensorFlow Summit Recap&lt;/a&gt; &lt;/strong&gt; from &lt;strong&gt;&lt;a target=&quot;_blank&quot; href=&quot;//www.slideshare.net/jiangjun1990&quot;&gt;Jiang Jun&lt;/a&gt;&lt;/strong&gt; &lt;/div&gt;

&lt;h2 id=&quot;download-slides&quot;&gt;Download Slides&lt;/h2&gt;

&lt;p&gt;https://pan.baidu.com/s/1miHh22W&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/tensorflow-summit-recap&quot;&gt;GDG Shanghai - 2017 TensorFlow Summit Recap&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on February 18, 2017.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[dt (docker-tool)]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/docker-tool" />
  <id>http://ohmystack.com/articles/docker-tool</id>
  <published>2017-02-04T00:00:00+08:00</published>
  <updated>2017-02-04T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;https://github.com/ohmystack/docker-tool&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/ohmystack/docker-tool&quot;&gt;dt (docker-tool)&lt;/a&gt; is a command-line tool for Docker operations.&lt;/p&gt;

&lt;p&gt;It is not like many other tools, you don’t need to build it. All of it is a single bash. Magic!&lt;br /&gt;
I have been using it for nearly 1 year, and it improves my efficiency of using Docker distinctly.&lt;/p&gt;

&lt;h2 id=&quot;its-not-just-an-alias&quot;&gt;It’s not just an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alias&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt&lt;/code&gt; gives you more commands that Docker doesn’t give you.&lt;/p&gt;

&lt;h4 id=&quot;dt-net&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt net&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;Get the network type and info.&lt;br /&gt;
You can even get the &lt;strong&gt;veth pair info&lt;/strong&gt; of a container if it is using the “default” NetworkMode. This is very helpful when debugging the network.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2017-02-04-docker-tool/dt-net-default.png&quot; alt=&quot;dt-net-default&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2017-02-04-docker-tool/dt-net-host.png&quot; alt=&quot;dt-net-host&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;dt-ns-net&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt ns-net&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;Enter the network namespace of a container.&lt;br /&gt;
So that you can use the utils installed on your server to debug the container inside network.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2017-02-04-docker-tool/dt-ns-net.png&quot; alt=&quot;dt-ns-net&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;dt-ssh&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt ssh&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;Go inside both container or image, automatically choose to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bash&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sh&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Before this, you always type&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker exec -it xxx /bin/bash&lt;/code&gt;,
and then find that there is no &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bash&lt;/code&gt; in the container, then change to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sh&lt;/code&gt;.
Or, type&lt;br /&gt;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker run -it --rm --entrypoint /bin/bash xxx&lt;/code&gt; to get into an image.
Now, you only need the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt ssh&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2017-02-04-docker-tool/dt-ssh-container.png&quot; alt=&quot;dt-ssh-container&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2017-02-04-docker-tool/dt-ssh-image.png&quot; alt=&quot;dt-ssh-image&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;powerful-shortcuts&quot;&gt;Powerful shortcuts&lt;/h2&gt;

&lt;p&gt;If you have tired with repeating typing the following commands, you can try the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt&lt;/code&gt; ones.&lt;/p&gt;

&lt;h4 id=&quot;dt-pid&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt pid&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;Get the pid of a container.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2017-02-04-docker-tool/dt-pid.png&quot; alt=&quot;dt-pid&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;dt-ps--dt-img&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt ps&lt;/code&gt; &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt img&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;Search (or list) the containers and images.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2017-02-04-docker-tool/dt-ps.png&quot; alt=&quot;dt-ps&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2017-02-04-docker-tool/dt-img.png&quot; alt=&quot;dt-img&quot; /&gt;&lt;/p&gt;

&lt;h4 id=&quot;dt-logs&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt logs&lt;/code&gt;&lt;/h4&gt;

&lt;p&gt;Short for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker logs --tail=50 -f &amp;lt;container-id/name&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;More features, you can check &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dt help&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker-tool&lt;/code&gt;, please reward the repo a STAR. 🌟&lt;/p&gt;

&lt;p&gt;https://github.com/ohmystack/docker-tool&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/docker-tool&quot;&gt;dt (docker-tool)&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on February 04, 2017.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[gotool]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/gotool" />
  <id>http://ohmystack.com/articles/gotool</id>
  <published>2015-12-03T00:00:00+08:00</published>
  <updated>2015-12-03T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;Golang 通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$GOPATH&lt;/code&gt; 来决定优先用哪里的源码。而这也就是 workspace。&lt;/p&gt;

&lt;p&gt;开发者往往需要多个 workspace。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一个 global 的 workspace&lt;/li&gt;
  &lt;li&gt;一个自己常用的开发环境的 workspace&lt;/li&gt;
  &lt;li&gt;还有一个，在调试别人项目的时候，需要载入别人 Godeps 下的 workspace&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;而切换这些 workspace 都靠 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;export $GOPATH=xxx:$GOPATH&lt;/code&gt; ，这样不够方便，而且容易搞糊涂，不能明显知道自己到底在哪个环境里。另外，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;go install&lt;/code&gt; 后，还依赖 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; 中要加入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;workspace&amp;gt;/bin&lt;/code&gt; 才能立即使用所安装的程序。&lt;/p&gt;

&lt;p&gt;所以，受 Python 里 virtualenv 工具的启发，写了这个工具：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gotool&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;项目地址：https://github.com/ohmystack/gotool&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2015-12-03-gotool-recording.gif&quot; alt=&quot;recording&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;安装&quot;&gt;安装&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://github.com/ohmystack/gotool.git
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;gotool
&lt;span class=&quot;nb&quot;&gt;cp &lt;/span&gt;gotool /usr/local/bin/gotool
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;使用&quot;&gt;使用&lt;/h2&gt;

&lt;h3 id=&quot;activate-a&quot;&gt;Activate (a)&lt;/h3&gt;

&lt;p&gt;进到 workspace 所在的目录，执行&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source &lt;/span&gt;gotool a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;完成 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$GOPATH&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; 的设置，而且命令行的开头会显示当前 workspace 的名字。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2015-12-03-gotool-workspace1.png&quot; alt=&quot;workspace&quot; /&gt;&lt;/p&gt;

&lt;p&gt;它会检查该目录下是否有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src/&lt;/code&gt;，只有存在 src 目录，才能被 activate。&lt;/p&gt;

&lt;p&gt;它还会检查这个 workspace 是不是在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Godeps/_workspace&lt;/code&gt; 下面，如果发现这是某个项目 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Godeps&lt;/code&gt; 的 workspace，它在命令行开头显示的，将是这个项目的名称。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2015-12-03-gotool-workspace2.png&quot; alt=&quot;workspace&quot; /&gt;&lt;/p&gt;

&lt;p&gt;自己在哪个环境里，一目了然。&lt;/p&gt;

&lt;h3 id=&quot;deactivate-d&quot;&gt;Deactivate (d)&lt;/h3&gt;

&lt;p&gt;使用后，如果想恢复到使用前的环境。只需要，deactivate 即可。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;source &lt;/span&gt;gotool d
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2015-12-03-gotool-deactivate.png&quot; alt=&quot;workspace&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;其它功能&quot;&gt;其它功能&lt;/h3&gt;

&lt;p&gt;这个工具现在其实是一个 bash 脚本，主要做上面这两个功能。你如果有什么好的想法，觉得可以再加入什么功能，欢迎 fork 这个项目，并把你的创意提交进来。&lt;/p&gt;

&lt;p&gt;希望你喜欢这个工具，让它帮助你在 terminal 开发 Golang 更加方便。&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/gotool&quot;&gt;gotool&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on December 03, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Nova Scheduler]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/nova-scheduler" />
  <id>http://ohmystack.com/articles/nova-scheduler</id>
  <published>2015-08-15T00:00:00+08:00</published>
  <updated>2015-08-15T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;get_filtered_hosts&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Used at `nova/scheduler/filter_scheduler.py`
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;HostManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_filtered_objects&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter_classes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hosts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;filter_properties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Used at `HostManager.get_filtered_hosts`
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;这里的 hosts 是一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HostState&lt;/code&gt; 的 list&lt;/p&gt;

&lt;p&gt;默认通常我们用下面这些 filters&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;ipdb&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pp&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CONF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scheduler_default_filters&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'RetryFilter'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;'AvailabilityZoneFilter'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;'RamFilter'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;'CoreFilter'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;'ComputeFilter'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;'ComputeCapabilitiesFilter'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;'ImagePropertiesFilter'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;'NumInstancesFilter'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;何为-filter&quot;&gt;何为 filter？&lt;/h3&gt;

&lt;p&gt;基类在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/filters.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BaseFilterHandler&lt;/code&gt; 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_filtered_objects&lt;/code&gt; 就是让 xxxFilter 执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter_all(objs, filter_properties)&lt;/code&gt;，
而 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter_all&lt;/code&gt; 里面是循环执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_filter_one&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;整个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FilterHandler&lt;/code&gt; 返回的结果再被下一个 filter 过滤。&lt;/p&gt;

&lt;p&gt;nova-scheduler (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/scheduler/filters/__init__.py&lt;/code&gt;) 有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BaseHostFilter&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HostFilterHandler&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HostFilterHandler&lt;/code&gt; 里面的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;host_passes(self, host_state, filter_properties)&lt;/code&gt; 方法，如果 filter 通过，则返回 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;True&lt;/code&gt;，这个 host 就被允许进入下一轮过滤。&lt;/p&gt;

&lt;h4 id=&quot;retryfilter&quot;&gt;&lt;strong&gt;RetryFilter&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;如果允许 retry，那么在传进来的 filter_properties 里，会有 ‘retry’ 字段。
类似 ‘retry’: {‘hosts’: [], ‘num_attempts’: 1},&lt;/p&gt;

&lt;h4 id=&quot;availabilityzonefilter&quot;&gt;&lt;strong&gt;AvailabilityZoneFilter&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;如果 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter_properties&lt;/code&gt; 里对 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;availability_zone&lt;/code&gt; 有要求。
则与 host 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aggregate_metadata&lt;/code&gt; 里 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;availability_zone&lt;/code&gt; 进行比较。
如果没有，则与 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CONF.default_availability_zone&lt;/code&gt; 进行比较。&lt;/p&gt;

&lt;h4 id=&quot;corefilter&quot;&gt;&lt;strong&gt;CoreFilter&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;根据 host 上还可用的 vcpu 数量进行过滤。&lt;/p&gt;

&lt;h4 id=&quot;computefilter&quot;&gt;&lt;strong&gt;ComputeFilter&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service_is_up&lt;/code&gt; 且没有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;host_state.service['disabled']&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;computecapabilitiesfilter&quot;&gt;&lt;strong&gt;ComputeCapabilitiesFilter&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;检查 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;instance_type&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;imagepropertiesfilter&quot;&gt;&lt;strong&gt;ImagePropertiesFilter&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;根据 HostState 里的 capabilities，与 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter_properties&lt;/code&gt; 里 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;image.properties&lt;/code&gt; 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;request_spec.image.properties&lt;/code&gt; 进行过滤。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;image_props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'architecture'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;image_props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'hypervisor_type'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;image_props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'vm_mode'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;numinstancesfilter&quot;&gt;&lt;strong&gt;NumInstancesFilter&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;检查 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;host_state.num_instances &amp;lt; CONF.max_instances_per_host&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;其实这几个自带的 Filter 都很容易，不用在这里多介绍，直接去看源码就好了。&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/nova-scheduler&quot;&gt;Nova Scheduler&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on August 15, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Nova API]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/nova-api" />
  <id>http://ohmystack.com/articles/nova-api</id>
  <published>2015-08-14T00:00:00+08:00</published>
  <updated>2015-08-14T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;blockquote&gt;
  &lt;p&gt;http://docs.openstack.org/developer/nova/devref/addmethod.openstackapi.html&lt;br /&gt;
https://www.ibm.com/developerworks/community/blogs/e93514d3-c4f0-4aa0-8844-497f370090f5/entry/openstack_nova_api?lang=en&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova.wsgi.Server&lt;/code&gt; 为每一个 Nova API worker 进程提供一个封装的 web server。它封装并管理一个 eventlet 优化过(greened) 的 WSGI server，它运行在一个自己管理的 green thread pool（这个线程池的大小可配）和一个给定的 socket （port、最大队列连接数可配）上。&lt;/p&gt;

&lt;p&gt;详见前面的文章 &lt;a href=&quot;/articles/nova-service&quot;&gt;了解 WSGIService nova-api 如何起来&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova.api.openstack.compute.APIRouter&lt;/code&gt; 用来允许将多个 WSGI applications 绑定到一个单独的 WSGI server 上，这也是为什么每个核心的 API 和插件(extension)资源都是一个个独立的 WSGI application。&lt;/p&gt;

&lt;p&gt;它继承自 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova.api.openstack.APIRouter&lt;/code&gt;，这个父类会在初始化的时候寻找 ExtensionManager、用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova.api.openstack.__init__&lt;/code&gt; 里的 ProjectMapper 类
创建 mapper（ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mapper.resource(member_name, collection_name, ...)&lt;/code&gt; 它将用于把属于一个 collection 的请求都映射到一个 controller 上），
并依次 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_setup_routes&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_setup_ext_routes&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_setup_extensions&lt;/code&gt;，其中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_setup_routes&lt;/code&gt; 这一关键方法必须由子类来实现，
这里定义了详细的基本 route 策略：mapper 中的每一个 route 必须指定一个 WSGI app 作为它的 controller。
最后，用它父类 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base_wsgi.Router&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/wsgi.py&lt;/code&gt;) 里的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__init__&lt;/code&gt; 方法，将刚刚生成的 mapper 传进去。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova.wsgi.Router&lt;/code&gt; 是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova.api.openstack.compute.APIRouter&lt;/code&gt; 的父类，在内部用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;routes.middleware.RoutesMiddleware&lt;/code&gt; 来把那些进来的请求映射到那些 WSGI applications。&lt;/p&gt;

&lt;p&gt;在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/api/openstack/wsgi.py&lt;/code&gt; 中还有很多 wsgi 通讯时的实现，比如：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对 Request 的简单封装，加入一些简单的 cache；&lt;/li&gt;
  &lt;li&gt;定义了一些解析、生成 body 的类：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TextDeserializer&lt;/code&gt;；&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DictSerializer&lt;/code&gt;；&lt;/li&gt;
  &lt;li&gt;定义了一些添加 (De)serializer 的装饰器；&lt;/li&gt;
  &lt;li&gt;什么是一个 action 的装饰器 @action；&lt;/li&gt;
  &lt;li&gt;继承某个 action 的装饰器 @extends(xxx)；ResponseObject。。。&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;resource-类&quot;&gt;Resource 类&lt;/h4&gt;

&lt;p&gt;它自己是一个 WSGI app，管理着 (de)serialization 和 controller 的分发。
读取由 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RoutesMiddleware&lt;/code&gt; 提供的 routing 信息，根据它的 controller 调用请求的 action。
所有的 controller 的 action 方法都必须接受一个叫 ‘req’ 的参数，内容是进来的 wsgi.Request 请求。
如果这个操作是 PUT 或是 POST，那么 controller 的方法就必须接受一个 ‘body’ 参数（内容为请求的 body）。
这些方法最后要么报一个 webob.exc 异常，要么返回一个 dict，这个字典将被按照要求的 content type 序列化。请细看 _process_stack(xxx) 这个方法。&lt;/p&gt;

&lt;h4 id=&quot;controller-类&quot;&gt;Controller 类&lt;/h4&gt;

&lt;p&gt;默认的 Controller 类。&lt;/p&gt;

&lt;p&gt;（这里待完善）&lt;/p&gt;

&lt;h4 id=&quot;调用过程&quot;&gt;调用过程&lt;/h4&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;openstack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wsgi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;890&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__call__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;889&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_process_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;--&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;890&lt;/span&gt;                                &lt;span class=&quot;n&quot;&gt;content_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accept&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;891&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;openstack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wsgi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;942&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_process_stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;941&lt;/span&gt;                 &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ResourceExceptionHandler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;--&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;942&lt;/span&gt;                     &lt;span class=&quot;n&quot;&gt;action_result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dispatch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;action_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;943&lt;/span&gt;             &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Fault&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;openstack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wsgi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1022&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dispatch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
   &lt;span class=&quot;mi&quot;&gt;1021&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1022&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;action_args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
   &lt;span class=&quot;mi&quot;&gt;1023&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;openstack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;servers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;480&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;479&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;--&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;480&lt;/span&gt;             &lt;span class=&quot;n&quot;&gt;servers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_get_servers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_detail&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;481&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;except&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Invalid&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;openstack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;servers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;564&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_get_servers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;563&lt;/span&gt;             &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_add_instance_faults&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;--&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;564&lt;/span&gt;             &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_view_builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;565&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;openstack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;views&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;servers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;135&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;detail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;134&lt;/span&gt;         &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Detailed view of a list of instance.&quot;&quot;&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;--&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;135&lt;/span&gt;         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_list_view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instances&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;136&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;openstack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;views&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;servers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;139&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_list_view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;138&lt;/span&gt;         &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Provide a view for a list of servers.&quot;&quot;&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;--&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;139&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;server_list&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;server&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;server&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;servers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;140&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;servers_links&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_get_collection_links&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nova&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;api&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;openstack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compute&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;views&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;servers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/nova-api&quot;&gt;Nova API&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on August 14, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Nova Service]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/nova-service" />
  <id>http://ohmystack.com/articles/nova-service</id>
  <published>2015-08-12T00:00:00+08:00</published>
  <updated>2015-08-12T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;h1 id=&quot;service&quot;&gt;Service&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/service.py&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;整个服务创建的过程，由一个进程变出多个子进程，再到运行起来，一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;server&lt;/code&gt; 贯穿始终，成为主线。这里的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;server&lt;/code&gt; 是一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Service&lt;/code&gt; 对象，名字容易让人与 api 里的 server 混淆。&lt;/p&gt;

&lt;p&gt;以 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova-scheduler&lt;/code&gt; 这个服务为例，它是从 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/cmd/scheduler.py&lt;/code&gt; （以前是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bin/nova-scheduler&lt;/code&gt;） 启动整个 nova-scheduler 服务。它通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/service.py&lt;/code&gt; 的类方法 create 创建了一个 topic 为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova.scheduler.rpcapi&lt;/code&gt; 的 service，即后面会提到的 server。在创建时，还显式提供了一个 ‘binary’ 参数，它只是一个以 ‘nova-‘ 开头的字符串，用于给其它参数赋上默认的参数，比如 binary 为 ‘nova-scheduler’，如果没有提供 manager 参数，那么就会默认去 CONF 里找名为 xxx_manager 的类，把它 import 作为 manager。Service 的 create 的全部参数如下：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  host             &lt;span class=&quot;c&quot;&gt;# defaults to CONF.host&lt;/span&gt;
  binary           &lt;span class=&quot;c&quot;&gt;# defaults to basename of executable&lt;/span&gt;
  topic            &lt;span class=&quot;c&quot;&gt;# defaults to bin_name - 'nova-' part&lt;/span&gt;
  manager          &lt;span class=&quot;c&quot;&gt;# defaults to CONF.&amp;lt;topic&amp;gt;_manager&lt;/span&gt;
  report_interval  &lt;span class=&quot;c&quot;&gt;# defaults to CONF.report_interval&lt;/span&gt;
  periodic_enable  &lt;span class=&quot;c&quot;&gt;# defaults to CONF.periodic_enable&lt;/span&gt;
  periodic_fuzzy_delay  &lt;span class=&quot;c&quot;&gt;# defaults to CONF.periodic_fuzzy_delay&lt;/span&gt;
  periodic_interval_max &lt;span class=&quot;c&quot;&gt;# if set, the max time to wait between runs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;一个 Service 通过一个 manager 监听某个 topic 的 queue 来实现 rpc。Service 会通过 manager 运行定时任务(periodical tasks)，并向数据库的 service 表报告当前状态。&lt;/p&gt;

&lt;p&gt;一个 Service 创建好后，需要让它运行起来。于是执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/service.py&lt;/code&gt; 最后面的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;serve(xxx)&lt;/code&gt; 方法，它会新生成一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProcessLauncher&lt;/code&gt; 对象（这个类也是在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.py&lt;/code&gt; 中定义的），用来为这个 service 管理进程。然后，调用这个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProcessLauncher&lt;/code&gt; 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;launch_server(server, workers=&amp;lt;int&amp;gt;)&lt;/code&gt; 函数（注意是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ProcessLauncher&lt;/code&gt; 类里面的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;launch_server&lt;/code&gt; 方法），启动 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;int&amp;gt;&lt;/code&gt; 个 worker（子进程），父进程只起管理子进程的功能，子进程的信息全部存在父进程所在的 wrap 里，子进程是真正干活的。子进程会执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_child_process(server)&lt;/code&gt; 方法。&lt;/p&gt;

&lt;p&gt;在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_child_process(server)&lt;/code&gt; 方法中，把子进程传进去。在对进程信号和 eventlet 进行开启。然后创建一个 Launcher 对象，把子进程的 server 扔进去，通过 Launcher 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;run_server&lt;/code&gt; 静态方法，把 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;server.start()&lt;/code&gt; 并让它 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;server.wait()&lt;/code&gt;。直到运行结束或出错抛出异常。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;到这里，一个 service 就运行起来了。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/service.py&lt;/code&gt; 的其它类中，充满了各种利用进程信号量的处理。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;signal.signal(xxx,XXX)&lt;/code&gt; 方法可以将系统对 xxx 事件的处理方法改为 XXX。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 几个常用信号:&lt;/span&gt;

SIGINT    终止进程  中断进程 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;control+c&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
SIGTERM   终止进程  软件终止信号 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;比较友好，如果进程不能中断，可以忽略这个信号&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
SIGKILL   终止进程  杀死进程 &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;这个信号不能被忽略，让进程立刻停止&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
SIGALRM   闹钟信号
SIGCHLD   一个子进程结束后自动向父进程发送SIGCHLD信号。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 两种标准信号处理方法：&lt;/span&gt;

signal.SIG_DFL  按系统默认的对该信号处理的方法进行处理。
signal.SIG_IGN  忽略该信号。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;关于 signal 的详细用法，参考：&lt;br /&gt;
http://docs.python.org/2/library/signal.html&lt;br /&gt;
http://blog.csdn.net/liangguohuan/article/details/7099978&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;关于 service 的 worker 的创建(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_start_child&lt;/code&gt; 方法)，首先确保子进程不会被太快地 fork 出来，保证一秒一个。在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pid = os.fork()&lt;/code&gt; 这句执行时，子进程就被创建出来了，子进程在此时的 pid 变量值会被赋为 0，父进程的 pid 变量的值为子进程的系统真实 pid。&lt;/p&gt;

&lt;p&gt;接下来，父进程和子进程都开始执行下一句代码，但是两者是异步的。所以接下来的一句 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if pid == 0:&lt;/code&gt; 就会把父子进程区别对待，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; 里的代码只会对子进程起作用，来防止子进程循环创建子进程。&lt;/p&gt;

&lt;p&gt;最后，其实可以细看一下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/service.py&lt;/code&gt; 里 Service 类的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;start()&lt;/code&gt; 方法，它将通过 manager 创建 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dispathcer&lt;/code&gt;，用它创建出 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;consumer&lt;/code&gt;。并创建出定时任务。&lt;/p&gt;

&lt;h1 id=&quot;wsgi-service&quot;&gt;WSGI Service&lt;/h1&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova-api&lt;/code&gt; service 跟上面的 Service 不同，它是通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova.service.WSGIService&lt;/code&gt; 类起来的。&lt;/p&gt;

&lt;p&gt;它依次把 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova.conf&lt;/code&gt; 里的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enabled_apis = ec2, osapi_compute, metadata&lt;/code&gt; 模块载入。
载入的时候，在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WSGIService&lt;/code&gt; 里，会尝试载入名为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xxx_manager&lt;/code&gt; 的 class。&lt;/p&gt;

&lt;h3 id=&quot;wsgiservice-nova-api-如何起来&quot;&gt;WSGIService nova-api 如何起来&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova.cmd.api -&amp;gt; main()&lt;/code&gt; 启动 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;api='osapi_compute'&lt;/code&gt; 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;WSGIService&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;由于不存在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;osapi_compute_manager&lt;/code&gt; 这样的类，所以 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self.manager&lt;/code&gt; 为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;None&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;所以就在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__init__&lt;/code&gt; 里初始化了&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wsgi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后直接在 start 时执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self.server.start()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wsgi.Server&lt;/code&gt; 这个对象 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__init__&lt;/code&gt; 的时候，&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_socket&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;eventlet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;listen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bind_addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;family&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;backlog&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backlog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;向系统申请了指定 port 的 socket 资源。其中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;backlog&lt;/code&gt; 是 The maximum number of queued connections.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wsgi.Server.start()&lt;/code&gt; 时，经过对 socket 的一些列准备（复制一份、设置 SO_KEEPALIVE、wrap ssl …），
最后用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eventlet.spawn(func, *args, **kwargs)&lt;/code&gt; 把服务起起来。&lt;/p&gt;

&lt;p&gt;而这里的：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;func&lt;/code&gt; 就是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eventlet.wsgi.server&lt;/code&gt;，这个方法就是 eventlet 根据 sock、site、custom_pool、log_format 等参数来起 http server。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;参数里：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app&lt;/code&gt; 是上面 Service 传进来的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self.loader.load_app(name)&lt;/code&gt;，Return the paste URLMap wrapped WSGI application.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;protocol&lt;/code&gt; 默认是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eventlet.wsgi.HttpProtocol&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;backlog&lt;/code&gt; 默认是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;128&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;custom_pool&lt;/code&gt; 是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eventlet.GreenPool(self.pool_size)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/nova-service&quot;&gt;Nova Service&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on August 12, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Nova RPC]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/nova-rpc" />
  <id>http://ohmystack.com/articles/nova-rpc</id>
  <published>2015-08-06T00:00:00+08:00</published>
  <updated>2015-08-06T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;blockquote&gt;
  &lt;p&gt;继之前介绍 &lt;a href=&quot;/articles/oslo-messaging&quot;&gt;oslo.messaging 笔记&lt;/a&gt;，这次专门来看看 Nova 里 RPC 的使用和实现。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1 id=&quot;overview&quot;&gt;Overview&lt;/h1&gt;

&lt;p&gt;每个 Nova 服务都会在初始化的时候创建 2 个 queue，一个是以 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NODE-TYPE.NODE-ID&lt;/code&gt; (比如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;compute.hostname&lt;/code&gt;) 为 key 来接受消息，另一个是以总称 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NODE-TYPE&lt;/code&gt; (for example &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;compute&lt;/code&gt;) 为 key 来接受消息。前者，是当 Nova-API 需要将命令进行重定向的时候用的，比如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;euca-terminate instance&lt;/code&gt; 这个命令只有传递给运行着指定 vm 的 compute 节点才能生效。&lt;/p&gt;

&lt;p&gt;当 RPC 是 ‘请求/相应’ 模式(call)的时候，API 的行为就是一个 ‘消费者(consumer)’，其它情况下，所有的 API 行为都是一个 Publisher。&lt;/p&gt;

&lt;p&gt;一个 RabbitMQ 节点就是一个 message broker(中间人)。每一个 Nova 组件都连接到一个 message broker，然后根据它们自己的属性 (比如是 compute node 或是 network node)，就可以使用对应的 queue，这样它自己就变成了一个 Invoker(调用者, 比如 API 或者 Scheduler) 或者是一个 Worker(真正做事的人，比如 Compute 或者 Network)。&lt;/p&gt;

&lt;p&gt;在 Nova 的对象里，是没有 Invoker 和 Worker 的，这两个名字只是为了方便理解。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Invoker&lt;/strong&gt;: 就是一个在消息队列系统中发消息的组件。通过 rpc.call 或 rpc.cast 发送。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Worker&lt;/strong&gt;: 就是一个从消息队列系统中接受消息的组件，如果消息是 rpc.call 发出的，它然后还要回复这个消息。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;topic-publisher&quot;&gt;Topic Publisher&lt;/h3&gt;

&lt;p&gt;生命周期从 rpc.call 或 rpc.cast 开始。它被生成用来 push 一个消息到消息队列。每个 Publisher 连的 topic-based exchange 是不会变的。它的生命周期仅限于这个消息的传递。&lt;/p&gt;

&lt;h3 id=&quot;direct-consumer&quot;&gt;Direct Consumer&lt;/h3&gt;

&lt;p&gt;生命周期只会从一个 rpc.call 被执行开始。它用来从消息队列接收消息。每一个这种 consumer 通过一个唯一指定的 queue 连接到一个唯一指定的 direct-based exchange。它的生命周期也仅限于这个消息的传递，exchange 和 queue 身份的识别是通过一个 UUID 生成器，这些都封装在 Topic Publisher 发出的这个 call 消息里。&lt;/p&gt;

&lt;h3 id=&quot;topic-consumer&quot;&gt;Topic Consumer&lt;/h3&gt;

&lt;p&gt;它的生命周期会跟某个 Worker 是同步的。它用来从队列接收信息，并启动定义在 Worker 里正确对应的方法。一个 Topic Consumer 通过一个共享的 queue 或是一个独立的 queue 连到一个对应 topic-based exchange 上。&lt;/p&gt;

&lt;p&gt;每个 Worker 会有两个 topic consumer：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;一个是处理 rpc.cast，它是连接到一个共享的 queue，exchange 的 key 是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;topic&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;另一个是处理 rpc.call，它连接到一个唯一的 queue，exchange 的 key 是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;topic.host&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;direct-publisher&quot;&gt;Direct Publisher&lt;/h3&gt;

&lt;p&gt;rpc.call 的行为产生一个 Direct Publisher，它实例化后就返回在 “request/response” 操作里需要的消息。它连到一个 direct-based exchange，在传来的消息里保存着字典化后的身份信息。&lt;/p&gt;

&lt;h3 id=&quot;topic-exchange&quot;&gt;Topic Exchange&lt;/h3&gt;

&lt;p&gt;Exchange 是一张路由表，它存放在一个 virtual host 的 context 里，它的多租户机制由 Qpid 或者 RabbitMQ 本身来提供。它的类型(是 topic 还是 direct)决定了路由的策略。在 Nova 里，一个 message broker 节点为每一个(or 所有？) topic 只会提供一个 topic-based exchange。&lt;/p&gt;

&lt;h3 id=&quot;direct-exchange&quot;&gt;Direct Exchange&lt;/h3&gt;

&lt;p&gt;这张路由 表是指 rpc.call 的操作过程中被创建。在一个 message broker 节点的生命周期中会生成很多个这样的 Direct Exchange，一个 rpc.call 就会生成一个。&lt;/p&gt;

&lt;h3 id=&quot;queue-element&quot;&gt;Queue Element&lt;/h3&gt;

&lt;p&gt;队列就是一个消息筒，消息一直被保存着，直到一个 Consumer(无论是 Topic Consumer 还是 Direct Consumer) 连接到这个 queue，然后把消息拿走。Queue 可以是被共享的，也可以是独立的。相同功能(personality)的 Workers 共享着 queues，它们的 routing key 都是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;topic&lt;/code&gt;。&lt;/p&gt;

&lt;h1 id=&quot;call-cast-flow&quot;&gt;Call, Cast Flow&lt;/h1&gt;

&lt;p&gt;用 OpenStack 官方文档里的两张图&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2015-08-06-nova-rpc-call-flow.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2015-08-06-nova-rpc-cast-flow.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;detail-implementation&quot;&gt;Detail implementation&lt;/h1&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/openstack/common/rpc/impl_kombu.py&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/openstack/common/rpc/__init__.py&lt;/code&gt; 最后的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_get_impl()&lt;/code&gt; 方法，从 nova.conf 中读出 rpc_backend 的类路径，比如， &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova.openstack.common.rpc.impl_kombu&lt;/code&gt; ，然后载入。&lt;/p&gt;

&lt;p&gt;而基于 AMQP 的 rpc 实现，比如 impl_kombu 和 impl_qpid，都会用到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nova/openstack/common/rpc/amqp.py&lt;/code&gt; ，由它共享一些通用的方法和类，比如连接池、Callback 的一系列封装。&lt;/p&gt;

&lt;p&gt;每一种 rpc_backend 都会实现以下类：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    ConsumerBase   &lt;span class=&quot;c&quot;&gt;# 在一个 amqp channel 上声明一个 queue&lt;/span&gt;
    DirectConsumer &lt;span class=&quot;c&quot;&gt;# 一个 direct queue&lt;/span&gt;
    TopicConsumer  &lt;span class=&quot;c&quot;&gt;# 一个 topic queue&lt;/span&gt;
    FanoutConsumer &lt;span class=&quot;c&quot;&gt;# 一个 fanout queue&lt;/span&gt;
    Publisher      &lt;span class=&quot;c&quot;&gt;# 它提供与 rabbit 的 reconnect 和 send 方法&lt;/span&gt;
    DirectPublisher
    TopicPublisher
    FanoutPublisher
    NotifyPublisher
    Connection     &lt;span class=&quot;c&quot;&gt;# 最重要的类&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Connection&lt;/code&gt; 类的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reconnect(包含了第一次的 connect)、close、reset、declare_consumer&lt;/code&gt; 这几个方法，对整个 rpc 通讯的过程都至关重要。&lt;/p&gt;

&lt;p&gt;Connection 中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ensure&lt;/code&gt; 方法是一个比较巧妙的方法。它接收 2 个方法作为参数，一个是要执行的方法，另一个是前一个方法执行出错时调用的方法。这样就可以根据不同的错误执行不同的操作了，比如记录不同的 log。&lt;/p&gt;

&lt;p&gt;而上面的几个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xxxConsumer&lt;/code&gt; 类，也都在 Connection 中有对应的方法来创建出这类 consumer，比如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;declare_topic_consumer(...)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;那么，在这个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;impl_kombu&lt;/code&gt; 的最后，提供了对外最关键的一系列 rpc 方法：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    create_connection
    multicall      &lt;span class=&quot;c&quot;&gt;# Make a call that returns multiple times.&lt;/span&gt;
    call           &lt;span class=&quot;c&quot;&gt;# Sends a message on a topic and wait for a response.&lt;/span&gt;
    cast           &lt;span class=&quot;c&quot;&gt;# Sends a message on a topic without waiting for a response.&lt;/span&gt;
    fanout_cast    &lt;span class=&quot;c&quot;&gt;# Sends a message on a fanout exchange without waiting for a response.&lt;/span&gt;
    cast_to_server &lt;span class=&quot;c&quot;&gt;# Sends a message on a topic to a specific server.&lt;/span&gt;
    fanout_cast_to_server &lt;span class=&quot;c&quot;&gt;# Sends a message on a fanout exchange to a specific server.&lt;/span&gt;
    notify         &lt;span class=&quot;c&quot;&gt;# Sends a notification event on a topic.&lt;/span&gt;
    cleanup
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/nova-rpc&quot;&gt;Nova RPC&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on August 06, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[如何学习 OpenStack]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/how-to-learn-openstack" />
  <id>http://ohmystack.com/articles/how-to-learn-openstack</id>
  <published>2015-08-04T00:00:00+08:00</published>
  <updated>2015-08-04T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;blockquote&gt;
  &lt;p&gt;这是我在知乎上的一篇回答。&lt;br /&gt;
http://www.zhihu.com/question/33466516/answer/56968879&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;在玩之前先搞清楚 OpenStack 各组件是干什么的。&lt;/p&gt;

&lt;p&gt;然后，要有自己的环境才能开始玩。（先搞个梯子吧，不然下载代码会让你很痛苦。）&lt;br /&gt;
可以用 DevStack 试着搭一个 all-in-one 的。（以后再慢慢尝试单独的 Network Node。）&lt;br /&gt;
Devstack 的几个最基本的配置写好即可。（配置先搞最简单的，网络也用最简单的。）&lt;br /&gt;
目标只有一个，搭起来后，能从 Dashboard 创建虚拟机，而且虚拟机能拿到网络。&lt;br /&gt;
遇到困难，耐心地去查 OpenStack 各组件的文档，或者重新配 Devstack 的配置，重新来。这是整体了解 OpenStack 最好的过程。这时候基本上还不用看 OpenStack 的代码，最多看两眼 Devstack 的脚本，了解下填的参数被用到了哪里。&lt;/p&gt;

&lt;p&gt;环境好了以后，其实就是去体会各种角色。&lt;/p&gt;

&lt;h3 id=&quot;openstack-cloud-administrator&quot;&gt;OpenStack Cloud Administrator&lt;/h3&gt;

&lt;p&gt;可以玩一下 OpenStack 的各组件的命令，例如 &lt;a href=&quot;http://docs.openstack.org/cli-reference/content/novaclient_commands.html&quot;&gt;novaclient commands&lt;/a&gt;&lt;br /&gt;
现在，你的角色相当于是一位 &lt;strong&gt;OpenStack Cloud Administrator&lt;/strong&gt;。&lt;br /&gt;
可以看 &lt;a href=&quot;http://docs.openstack.org/admin-guide-cloud/content/ch_getting-started-with-openstack.html&quot;&gt;OpenStack Cloud Administrator Guide&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;然后，你可能会想玩玩更多的功能，配置更多的参数。&lt;br /&gt;
可以看 &lt;a href=&quot;http://docs.openstack.org/kilo/config-reference/content/&quot;&gt;OpenStack Configuration Reference&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;openstack-ops&quot;&gt;OpenStack OPS&lt;/h3&gt;

&lt;p&gt;但玩着玩着，肯定会出现问题，调不通了，甚至把环境玩坏了，这时候，你就要变身 &lt;strong&gt;OpenStack OPS&lt;/strong&gt; 了。&lt;br /&gt;
&lt;a href=&quot;http://docs.openstack.org/openstack-ops/content/openstack-ops_preface.html&quot;&gt;OpenStack Operations Guide&lt;/a&gt;
这里面就教你很多 troubleshooting 的技能，还有 monitoring、logging，甚至一点架构的设计。这些都是以后非常实用的技能。&lt;/p&gt;

&lt;h3 id=&quot;openstack-architect&quot;&gt;OpenStack Architect&lt;/h3&gt;

&lt;p&gt;说到架构，也有专门的文档 &lt;a href=&quot;http://docs.openstack.org/arch-design/content/ch_preface.html&quot;&gt;OpenStack Architecture Design Guide&lt;/a&gt;&lt;br /&gt;
这时，你就成了 &lt;strong&gt;IaaS 的架构师&lt;/strong&gt;。&lt;/p&gt;

&lt;h3 id=&quot;openstack-developer&quot;&gt;OpenStack Developer&lt;/h3&gt;

&lt;p&gt;而当你发现有 bug，有新需求，或者想了解细节的实现了，需要看代码了，你就变身 &lt;strong&gt;OpenStack Developer&lt;/strong&gt;。
可以看 &lt;a href=&quot;http://docs.openstack.org/developer/openstack-projects.html&quot;&gt;OpenStack - Python Developer Guide (docs)&lt;/a&gt;
对于开发者来说，这个文档有一定作用，但其实最好还是直接读源码。&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;还有更多文档，看 &lt;a href=&quot;http://docs.openstack.org/&quot;&gt;OpenStack Docs&lt;/a&gt; 这里的汇总。其实，我对里面 &lt;a href=&quot;http://docs.openstack.org/user-guide/&quot;&gt;OpenStack Docs: OpenStack End User Guide&lt;/a&gt; 这个文档充满期待，以后我觉得它会是很好的入门教材，但现在还在完善中，里面很多文章，其实都是原本别人用 MarkDown 写在 Github 的文章，后来被整理到这里。&lt;/p&gt;

&lt;p&gt;之后，就是不断搜集各个牛人的 RSS，多读人家的文章。以后再补充&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/how-to-learn-openstack&quot;&gt;如何学习 OpenStack&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on August 04, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Make your DVR playground using DevStack]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/devstack-dvr" />
  <id>http://ohmystack.com/articles/devstack-dvr</id>
  <published>2015-08-01T00:00:00+08:00</published>
  <updated>2015-08-01T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;h2 id=&quot;slideshare&quot;&gt;Slideshare&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;Last updated at: 2015/05/18&lt;/p&gt;
&lt;/blockquote&gt;

&lt;iframe src=&quot;//www.slideshare.net/slideshow/embed_code/key/67l5VSWOWgWuKd&quot; width=&quot;510&quot; height=&quot;420&quot; frameborder=&quot;0&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; scrolling=&quot;no&quot; style=&quot;border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;&quot; allowfullscreen=&quot;&quot;&gt; &lt;/iframe&gt;
&lt;div style=&quot;margin-bottom:5px&quot;&gt; &lt;strong&gt; &lt;a href=&quot;//www.slideshare.net/jiangjun1990/make-your-dvr-playground-using-devstack&quot; title=&quot;Make your DVR playground using DevStack&quot; target=&quot;_blank&quot;&gt;Make your DVR playground using DevStack&lt;/a&gt; &lt;/strong&gt; from &lt;strong&gt;&lt;a href=&quot;//www.slideshare.net/jiangjun1990&quot; target=&quot;_blank&quot;&gt;Jiang Jun&lt;/a&gt;&lt;/strong&gt; &lt;/div&gt;

&lt;h2 id=&quot;devstack-localconf&quot;&gt;DevStack local.conf&lt;/h2&gt;

&lt;p&gt;https://gist.github.com/ohmystack/93aa8215456124fb147c&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/devstack-dvr&quot;&gt;Make your DVR playground using DevStack&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on August 01, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[oslo.messaging 笔记]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/oslo-messaging" />
  <id>http://ohmystack.com/articles/oslo-messaging</id>
  <published>2015-07-20T00:00:00+08:00</published>
  <updated>2015-07-20T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;h2 id=&quot;消息消费者&quot;&gt;消息消费者&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service.start()&lt;/code&gt; 时，把 rpc 准备起来。&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rpcserver&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rpc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endpoints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rpcserver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;novarpcpy&quot;&gt;nova/rpc.py&lt;/h4&gt;

&lt;p&gt;里面 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_server&lt;/code&gt; 调用 oslo.messaging&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;   &lt;span class=&quot;n&quot;&gt;messaging&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_rpc_server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TRANSPORT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;endpoints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;executor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'eventlet'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;serializer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;serializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;Construct an RPC server.

    The executor parameter controls how incoming messages will be received and
    dispatched. By default, the most simple executor is used - the blocking
    executor.

    If the eventlet executor is used, the threading and time library need to be
    monkeypatched.

    :param transport: the messaging transport
    :type transport: Transport
    :param target: the exchange, topic and server to listen on
    :type target: Target
    :param endpoints: a list of endpoint objects
    :type endpoints: list
    :param executor: name of a message executor - for example
                     'eventlet', 'blocking'
    :type executor: str
    :param serializer: an optional entity serializer
    :type serializer: Serializer
    &quot;&quot;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;oslo_messagingrpcserverpy&quot;&gt;oslo_messaging/rpc/server.py&lt;/h4&gt;

&lt;p&gt;里面有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_rpc_server&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;它创建一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPCDispatcher&lt;/code&gt;，再把 transport, dispatcher, executor 结合，创建出一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;oslo_message.server.MessageHandlingServer&lt;/code&gt; 返回给调用者。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Server for handling messages.&lt;/p&gt;

  &lt;p&gt;Connect a transport to a dispatcher that knows how to process the message using an executor that knows how the app wants to create new tasks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;这个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MessageHandlingServer&lt;/code&gt; 是可以被 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.start()&lt;/code&gt; 的。Start handling incoming messages.&lt;/p&gt;

&lt;p&gt;它里面也是载入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;oslo_messsaging/_executors&lt;/code&gt; 里面的 driver(blocking, eventlet, …)，用里面的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.start()&lt;/code&gt; 起来的。&lt;/p&gt;

&lt;p&gt;而在创建这个 executor object 时，传入了 conf, listener, dispatcher。而这里的 listener 是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dispatcher._listen(self.transport)&lt;/code&gt;，而 dispatcher 里面又是让 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;transport._listen(self.target)&lt;/code&gt; 终于知道该听啥了。&lt;/p&gt;

&lt;p&gt;最后是调用这个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self._executor.start()&lt;/code&gt; 开始 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listener.poll()&lt;/code&gt; 坐等请求进来。&lt;/p&gt;

&lt;h4 id=&quot;oslo_messagingrpcdispatcherpy&quot;&gt;oslo_messaging/rpc/dispatcher.py&lt;/h4&gt;

&lt;p&gt;来了一个 RPC message，由 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPCDispatcher&lt;/code&gt; 来读懂格式，根据 namespace, version, method 把它匹配到对应的 endpoints。&lt;/p&gt;

&lt;p&gt;直接 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__call__&lt;/code&gt; 它的时候，
调用里面的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_dispatch_and_reply&lt;/code&gt;，
里面通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_dispatch&lt;/code&gt; 一些逻辑，检查是否有这种方法，如果有，
交给 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_do_dispatch&lt;/code&gt; 去解包 (deserialize) 得到 (consumer) 真正的 func 并执行。&lt;/p&gt;

&lt;p&gt;endpoint 暴露着 namespace + version 特定的 method。endpoint 的所有 public methods 都可以被 client 远程调用。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;消息生产者&quot;&gt;消息生产者&lt;/h2&gt;

&lt;h4 id=&quot;oslo_messagingrpcclientpy&quot;&gt;oslo_messaging/rpc/client.py&lt;/h4&gt;

&lt;p&gt;所谓 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;oslo_messaging/rpc/client.py&lt;/code&gt; 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPCClient&lt;/code&gt;
其实在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.prepare&lt;/code&gt; 时，是去用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_CallContext._prepare(...)&lt;/code&gt; 返回一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_CallContext&lt;/code&gt; 对象。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prepare&lt;/code&gt; 无非是设置一些参数、属性。&lt;/p&gt;

&lt;h4 id=&quot;oslo_messaging_driversamqpdriverpy&quot;&gt;oslo_messaging/_drivers/amqpdriver.py&lt;/h4&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMQPDriverBase&lt;/code&gt; 里面可以看到，由 kombu 支撑起来的核心方法就是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_send&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RPCClient&lt;/code&gt; 的 call, cast 其实都是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_CallContext&lt;/code&gt; 的方法。&lt;/p&gt;

&lt;p&gt;cast 就不说了，是靠 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self.transport._send()&lt;/code&gt; 实现的。
call 的功能，其实也是靠 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self.transport._send()&lt;/code&gt; 实现的。transport 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_send()&lt;/code&gt; 又是由 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self._driver.send(...)&lt;/code&gt; 完成的。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AMQPDriverBase&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;oslo_messaging/_drivers/amqpdriver.py&lt;/code&gt;) 里 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_send()&lt;/code&gt; 可以做很多事情，让 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_send&lt;/code&gt; 百变。
它能发 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;conn.noticy_send&lt;/code&gt;，能 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;conn.fanout_send&lt;/code&gt;，还能 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;conn.topic_send&lt;/code&gt;。
如果是 call，那它通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wait_for_reply&lt;/code&gt; 参数，知道要提前去准备好一个 msg_id 和 _reply_q&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_msg_id'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'_reply_q'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_get_reply_q&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;后者创建了一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReplyWaiter&lt;/code&gt; object。
在消息发出后，&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_waiter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wait&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;timeout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;oslo_messaging_driversimpl_rabbitpy&quot;&gt;oslo_messaging/_drivers/impl_rabbit.py&lt;/h4&gt;

&lt;p&gt;里面的 RabbitDriver 继承自 AMQPDriverBase&lt;/p&gt;

&lt;h4 id=&quot;oslo_messagingtransportpy&quot;&gt;oslo_messaging/transport.py&lt;/h4&gt;

&lt;p&gt;一个 Transport 只是 driver 的一层壳，把 driver 包起来。&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/oslo-messaging&quot;&gt;oslo.messaging 笔记&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on July 20, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[DevStack 笔记]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/devstack-notes" />
  <id>http://ohmystack.com/articles/devstack-notes</id>
  <published>2015-07-17T00:00:00+08:00</published>
  <updated>2015-07-17T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;h2 id=&quot;如何备份-optstack&quot;&gt;如何备份 /opt/stack&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;zcf ~/stack.tar.gz &lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'/opt/stack/data'&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'/opt/stack/status'&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'/opt/stack/tempest/.tox'&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;.venv &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;.log &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;.pyc &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
/opt/stack

&lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;cf ~/new_stack.tar stack &lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'stack/data'&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'stack/logs'&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'stack/status'&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'stack/.wheelhouse'&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;.pyc &lt;span class=&quot;nt&quot;&gt;--exclude&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;.venv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;误区&quot;&gt;误区&lt;/h2&gt;

&lt;p&gt;长时间用同一份 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/opt/stack&lt;/code&gt; 下面的源码。会引发一些 requirements 的问题。&lt;/p&gt;

&lt;h2 id=&quot;强制从-git-安装某些-client-包&quot;&gt;强制从 Git 安装某些 client 包&lt;/h2&gt;

&lt;p&gt;DevStack 安装那些主要服务时，默认会从 Git 拉下来安装；装其它那些 client 包时，就直接从 pypi 装了。如果希望强制某些 client 包也从 Git 安装，可以这样：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;LIBS_FROM_GIT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;python-novaclient,python-neutronclient,oslo.concurrency,oslo.messaging,oslo.serialization,oslo.utils
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;让-dashboard-拥有-vnc-console&quot;&gt;让 Dashboard 拥有 VNC Console&lt;/h2&gt;

&lt;p&gt;从某一版本对 DevStack 后，novnc 和 cauth 就从默认安装里去除了。所以，我们得手动加上。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;enable_service n-novnc n-cauth
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;cirros&quot;&gt;cirros&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;username: cirros
password: cubswin:)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/devstack-notes&quot;&gt;DevStack 笔记&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on July 17, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Salt (6) Salt & Windows]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/salt-6-salt-and-windows" />
  <id>http://ohmystack.com/articles/salt-6-salt-and-windows</id>
  <published>2015-07-12T00:00:00+08:00</published>
  <updated>2015-07-12T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;Salt Minion 在 Windows 上还是会有一些坑的。这里只能列举一些我遇到过的。&lt;/p&gt;

&lt;h2 id=&quot;pip&quot;&gt;pip&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip.installed&lt;/code&gt; 这样的 state 会执行失败，报 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;State 'pip.installed' found in SLS 'xxx' is unavailable&lt;/code&gt; 这样的错误。&lt;/p&gt;

&lt;p&gt;这是因为 Salt 用的是它自己的 portable Python，也有自己的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip.exe&lt;/code&gt; 。安装官方的说法，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip&lt;/code&gt; 的 State 时，需要显式指明 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cwd&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bin_env&lt;/code&gt; （pip.exe 的位置）。可以参考： https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.pip.html&lt;/p&gt;

&lt;p&gt;但我实际试下来，并不管用。我最后还是简单粗暴地用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd.run&lt;/code&gt; 来执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pip&lt;/code&gt; 命令。&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;install-requirements&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;cmd.run&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;pip install -r C:\xxx\requirements.txt&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;cwd&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;C:\Python27\Scripts&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;继续更新&quot;&gt;[继续更新…]&lt;/h4&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/salt-6-salt-and-windows&quot;&gt;Salt (6) Salt & Windows&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on July 12, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Salt (5) RESTful API：Salt-api]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/salt-5-salt-api" />
  <id>http://ohmystack.com/articles/salt-5-salt-api</id>
  <published>2015-07-08T00:00:00+08:00</published>
  <updated>2015-07-08T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2014.7&lt;/code&gt; 这个版本后，salt-api 模块的代码已被 merge 进 salt-master，有两种启动方式：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rest_wsgi&lt;/code&gt; 方式。直接配置、部署在 Apache/Nginx 上。(http://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_wsgi.html)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rest_cherrypy&lt;/code&gt; 方式。不依赖 Apache/Nginx，模块自己用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CherryPy&lt;/code&gt; 起了一个 Web Service。（据文档说，已符合生产使用的要求。）这种方式需要安装 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;salt-api&lt;/code&gt; 这个包。(http://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;试下来，选用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rest_cherrypy&lt;/code&gt; 支撑 Salt-api 能拥有更出色的性能。&lt;/p&gt;

&lt;p&gt;下面以 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rest_cherrypy&lt;/code&gt; 方式为例，起一个最简单的 HTTP（不用 HTTPS）的 Salt RESTful API 服务。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;salt-api
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;配置 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/master.d/salt-api.conf&lt;/code&gt;。&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;rest_cherrypy&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;8000&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;0.0.0.0&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;True&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;disable_ssl&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;接下来解决 API 请求的权限问题。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;salt-api&lt;/code&gt; 使用了 salt 的 &lt;a href=&quot;http://docs.saltstack.com/en/latest/topics/eauth/index.html#acl-eauth&quot;&gt;External Authentication System&lt;/a&gt; 。&lt;/p&gt;

&lt;p&gt;修改 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/master.d/salt-api.conf&lt;/code&gt;，配置 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;external_auth&lt;/code&gt;。&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;external_auth&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;pam&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;saltuser&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;.*&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@wheel'&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@runner'&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@jobs'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;上面就为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;saltuser&lt;/code&gt; 这个 Linux 用户赋予了几乎 salt 里所有的权限。&lt;/p&gt;

&lt;p&gt;重启 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;salt-master&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;salt-api&lt;/code&gt; 让配置生效&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;service salt-master restart
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;service salt-api restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后创建一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;saltuser&lt;/code&gt; 用户&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;adduser saltuser
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;至此，HTTP salt RESTful API 就搭好了。&lt;/p&gt;

&lt;p&gt;让我们试一下。&lt;/p&gt;

&lt;p&gt;首先在 salt-master 上直接用命检验一下这个用户的权限。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;salt &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; pam &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; test.ping
username: saltuser
password:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;看到正常的返回结果后，说明权限配置正确了。&lt;/p&gt;

&lt;p&gt;接下来试一下 RESTful API。&lt;/p&gt;

&lt;p&gt;有两种方式来发 HTTP 请求：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;每次请求里都带 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;username&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;password&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;第一次请求一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;token&lt;/code&gt;，后面就把它放进 HTTP 请求的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X-Auth-Token&lt;/code&gt; header 里。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;我们采用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;token&lt;/code&gt; 的方式。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl &lt;span class=&quot;nt&quot;&gt;-X&lt;/span&gt; POST &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
     &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Content-Type: application/x-www-form-urlencoded&quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
     &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Accept: application/json&quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
     &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'username=saltuser&amp;amp;password=xxxxxx&amp;amp;eauth=pam'&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
     http://10.18.5.147:8000/login
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;返回结果：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{
  &quot;return&quot;: [
    {
      &quot;perms&quot;: [
        &quot;.*&quot;,
        &quot;@wheel&quot;,
        &quot;@runner&quot;,
        &quot;@jobs&quot;
      ],
      &quot;start&quot;: 1421154569.16507,
      &quot;token&quot;: &quot;77af3748fb3385fdeedab4d7a6b70d94c36b7139&quot;,
      &quot;expire&quot;: 1421197769.165072,
      &quot;user&quot;: &quot;saltuser&quot;,
      &quot;eauth&quot;: &quot;pam&quot;
    }
  ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;有了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;token&lt;/code&gt;，来试一下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test.ping&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl http://10.18.5.147:8000/ &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
     &lt;span class=&quot;nt&quot;&gt;-X&lt;/span&gt; POST &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
     &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Accept: application/json&quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
     &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;X-Auth-Token: 77af3748fb3385fdeedab4d7a6b70d94c36b7139&quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
     &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Content-Type: application/json&quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
     &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;s1&quot;&gt;'[
  {
    &quot;client&quot;: &quot;local&quot;,
    &quot;tgt&quot;: &quot;*&quot;,
    &quot;fun&quot;: &quot;test.ping&quot;
  }
]'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;返回结果：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{
  &quot;return&quot;: [
    {
      &quot;jj-dev002.matrix.xxx.com&quot;: true,
      &quot;pc-dst52056&quot;: true,
      &quot;jj-dev001.matrix.xxx.com&quot;: true
    }
  ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;上面只是最简单的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test.ping&lt;/code&gt;，如果有更复杂的参数怎么办？&lt;/p&gt;

&lt;p&gt;可以看 http://docs.saltstack.com/en/latest/ref/clients/#salt.client.LocalClient.cmd 里面 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Parameters&lt;/code&gt; 这部分，知道有哪些参数可以写在 json 格式的 request body 里。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;搭建 salt HTTPS RESTful API 的文章&lt;/p&gt;

  &lt;p&gt;http://www.saltstack.cn/projects/cssug-kb/wiki/Salt-api-deploy-and-use
http://bencane.com/2014/07/17/integrating-saltstack-with-other-services-via-salt-api/&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;当然也有几个坑：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;重启 salt-master 和 salt-api 会让之前的 token 提前失效。&lt;/li&gt;
  &lt;li&gt;如果 token 失效，salt-api 的返回仍是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;200&lt;/code&gt;，只是内容里会提示 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Please log in&lt;/code&gt;。&lt;/li&gt;
&lt;/ol&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/salt-5-salt-api&quot;&gt;Salt (5) RESTful API：Salt-api&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on July 08, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Salt (4) 任务：Jobs]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/salt-4-jobs" />
  <id>http://ohmystack.com/articles/salt-4-jobs</id>
  <published>2015-06-30T00:00:00+08:00</published>
  <updated>2015-06-30T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;blockquote&gt;
  &lt;p&gt;http://docs.saltstack.com/en/latest/topics/jobs/&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;每次通过 salt 执行一个命令(salt module) / state / highstate，salt 都建一个 job。管理在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/var/cache/salt/proc&lt;/code&gt;。&lt;/p&gt;

&lt;h2 id=&quot;saltutil-封装的命令&quot;&gt;saltutil 封装的命令&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;running&lt;/code&gt; Returns the data of all running jobs that are found in the proc directory.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;find_job&lt;/code&gt; Returns specific data about a certain job based on job id.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;signal_job&lt;/code&gt; Allows for a given jid to be sent a signal.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;term_job&lt;/code&gt; Sends a termination signal (SIGTERM, 15) to the process controlling the specified job.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kill_job&lt;/code&gt; Sends a kill signal (SIGKILL, 9) to the process controlling the specified job.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;用法：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; saltutil.running
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;jobs-runner-封装的命令&quot;&gt;jobs runner 封装的命令&lt;/h2&gt;

&lt;p&gt;jobs runner 是专为操作 jobs 设计的命令。它的输出会比 saltutil 更易读，而且功能更多。如果你的命令是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--async&lt;/code&gt; 执行的，那么在知道 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jid&lt;/code&gt; 后，就需要 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;looup_jid&lt;/code&gt; 来不断尝试去获取执行结果了。&lt;/p&gt;

&lt;p&gt;用法：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 相当于 saltutil.running，结果更易读&lt;/span&gt;
salt-run jobs.active

&lt;span class=&quot;c&quot;&gt;# 列出所有 jobs，便于查找 jid (job id number)&lt;/span&gt;
salt-run jobs.list_jobs

&lt;span class=&quot;c&quot;&gt;# 查看某个 job 的运行结果&lt;/span&gt;
salt-run jobs.lookup_jid &amp;lt;job &lt;span class=&quot;nb&quot;&gt;id &lt;/span&gt;number&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/salt-4-jobs&quot;&gt;Salt (4) 任务：Jobs&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on June 30, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Salt (3) 核心所在：State]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/salt-3-state" />
  <id>http://ohmystack.com/articles/salt-3-state</id>
  <published>2015-06-20T00:00:00+08:00</published>
  <updated>2015-06-20T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.sls&lt;/code&gt; means &lt;strong&gt;S&lt;/strong&gt; a &lt;strong&gt;L&lt;/strong&gt; t &lt;strong&gt;S&lt;/strong&gt;tate file.&lt;/p&gt;

&lt;p&gt;它表示系统将会是什么状态。SLS 只是一些数据。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;State 的完整参考手册：
http://salt.readthedocs.org/en/latest/ref/states/index.html
一套完整的 state trees 可以称为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;formular&lt;/code&gt;，一些样例：
https://github.com/saltstack-formulas&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;salt-states-文件的存放位置&quot;&gt;Salt States 文件的存放位置&lt;/h2&gt;
&lt;p&gt;默认在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/srv/salt&lt;/code&gt;
可通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/master&lt;/code&gt; 中配置：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# /etc/salt/master&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;file_roots&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/srv/salt&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/mnt/salt-nfs/base&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;多环境配置&quot;&gt;多环境配置&lt;/h4&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# /etc/salt/master&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;file_roots&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/srv/salt/prod&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;qa&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/srv/salt/qa&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/srv/salt/prod&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;dev&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/srv/salt/dev&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/srv/salt/qa&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/srv/salt/prod&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;使用-topsls&quot;&gt;使用 top.sls&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;http://docs.saltstack.com/en/latest/ref/states/top.html&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;top.sls&lt;/code&gt; 用来管理哪些 sls 在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state.highstate&lt;/code&gt; 时允许被应用到哪些 hosts。&lt;/p&gt;

&lt;p&gt;默认的环境 (env) 叫 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base&lt;/code&gt;。下面的例子表示所有 host 都能使用 webserver.sls。&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# /srv/salt/top.sls&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;*'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;webserver&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;dev&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;dev-*'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;vim&lt;/span&gt;
  &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;db*dev*'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;db&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;qa&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;10.10.200.0/24'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;match&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ipcidr&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;deployments.qa.site1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果想上面那样，环境不止一个，那 states 如何工作呢？&lt;/p&gt;

&lt;p&gt;salt 寻找 states 文件是根据 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/master&lt;/code&gt; 中的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file_roots&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file_roots&lt;/code&gt; 根据 env 提供出各种 env 的文件路径（这就包括了告诉 salt &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;top.sls&lt;/code&gt; 在哪儿），而 env 本身却是通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;top.sls&lt;/code&gt; 定义的。那么，这就陷入了“鸡生蛋，蛋生鸡”的问题了。env 等着靠 top.sls 去划分，而 top.sls 自己却躲在在某个/各个 env 的目录下。要是每个 env 里都放了个 top.sls，那 salt 怎么知道该听哪个目录里的 top.sls？&lt;/p&gt;

&lt;p&gt;salt 为此定义了一套 &lt;a href=&quot;http://docs.saltstack.com/en/latest/ref/states/top.html#how-top-files-are-compiled&quot;&gt;top.sls 的寻找规则&lt;/a&gt;：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;如果有名为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base&lt;/code&gt; 的 env，且它的文件目录里有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;top.sls&lt;/code&gt;。（比如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/srv/salt/base/top.sls&lt;/code&gt;）这时其他任何的 top.sls 都将被无视，只取 base 里的这个 top.sls。&lt;/li&gt;
  &lt;li&gt;如果没有在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base&lt;/code&gt; 里配置 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;top.sls&lt;/code&gt;，那么，在剩下的几个 env 中，按 file_roots 中 env 名字的&lt;strong&gt;字母顺序&lt;/strong&gt;，依次检查它们目录下的 top.sls，找第一个含有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base&lt;/code&gt; 的那个 top.sls，一旦找到，其它 env 的 top.sls 都将被无视。&lt;/li&gt;
  &lt;li&gt;如果压根没有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;base&lt;/code&gt; 这个 env。就找对应 env 下的 top.sls，各个 env 自己负责自己的。如果这样没找到，那就又去按&lt;strong&gt;字母顺序&lt;/strong&gt;去别的 env 里找自己 env 的配置，找到为止。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;创建一个-sls-文件&quot;&gt;创建一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SLS&lt;/code&gt; 文件&lt;/h2&gt;

&lt;h4 id=&quot;sls-文件的命名规则&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SLS&lt;/code&gt; 文件的命名规则&lt;/h4&gt;

&lt;ol&gt;
  &lt;li&gt;使用时，忽略 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.sls&lt;/code&gt;。在 Salt 中使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xxx.sls&lt;/code&gt; 时，直接写 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;xxx&lt;/code&gt; 就可以了。&lt;/li&gt;
  &lt;li&gt;用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt; 连接多层目录。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aaa/bbb/haha.sls&lt;/code&gt; 写成 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aaa.bbb.haha&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;如果目录下有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init.sls&lt;/code&gt;，使用时只需写目录名称就可以了。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aaa/init.sls&lt;/code&gt; 写成 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aaa&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;如果同时存在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aaa.sls&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aaa/init.sls&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aaa&lt;/code&gt; 将只指向 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aaa.sls&lt;/code&gt;，忽略 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aaa/init.sls&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;建议不要跟 salt builtin state modules 有重名（比如，git）。否则在其它 sls 里 include 时会有报 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;The following requisites were not found&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4 id=&quot;sls-文件的内容&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SLS&lt;/code&gt; 文件的内容&lt;/h4&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;target_a&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;                         &lt;span class=&quot;c1&quot;&gt;# ID declaration&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;state_a&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;                        &lt;span class=&quot;c1&quot;&gt;# State declaration&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;state_func_a&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;some_value&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# function declaration&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;state_func_b&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;some_value&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;state_func_c&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;some_value&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;                    &lt;span class=&quot;c1&quot;&gt;# requisite declaration&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;pkg&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;xxx&lt;/span&gt;                  &lt;span class=&quot;c1&quot;&gt;# requisite reference&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;xxx/xxx.xx&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;state_b&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;                        &lt;span class=&quot;c1&quot;&gt;# Support multiple states&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;state_parameter_a&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;some_value&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;state_parameter_b&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;some_value&lt;/span&gt;
    
&lt;span class=&quot;na&quot;&gt;target_b&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;state_x&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;state_parameter_a&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;some_value&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;state_parameter_b&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;some_value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;target_a&lt;/code&gt; 可以是一个文件路径，可以是一个 service 的名字，……，可以是一切你希望控制的目标。（Salt 官方称它为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ID Declaration&lt;/code&gt;，但显然这个名称太误导人了。这可不是随便取取的什么 ID。不过，你的确可以随便写，然后在下面用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;- name: xxx&lt;/code&gt; 来显式指定 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ID Declaration&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;如果想指定多个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; 怎么办？用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;names&lt;/code&gt; ：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;python-pkgs&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;pkg.installed&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;names&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;python-django&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;python-crypto&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;python-yaml&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;原生-state-modules-列表&quot;&gt;原生 State modules 列表&lt;/h4&gt;

&lt;blockquote&gt;
  &lt;p&gt;http://docs.saltstack.com/en/latest/ref/states/all/index.html&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;state-执行条件执行顺序&quot;&gt;State 执行条件、执行顺序&lt;/h4&gt;

&lt;blockquote&gt;
  &lt;p&gt;http://docs.saltstack.com/en/latest/ref/states/requisites.html
http://docs.saltstack.com/en/latest/ref/states/ordering.html&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5 id=&quot;require&quot;&gt;require:&lt;/h5&gt;

&lt;p&gt;在执行这一步之前需要满足的东西都列在下面。不满足就不执行。它不会去修复它们。
甚至可以 require 整个 sls：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;foo&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;pkg.installed&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;sls&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;foo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;require_in&quot;&gt;require_in:&lt;/h5&gt;

&lt;p&gt;反过来指定 require。&lt;/p&gt;

&lt;h5 id=&quot;watch--watch_in&quot;&gt;watch / watch_in:&lt;/h5&gt;

&lt;p&gt;里面任何一个状态变化（有 change），就触发。
并不是所有 state 都支持 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;watch&lt;/code&gt;。（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;service state&lt;/code&gt; 能支持。）&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# 可以让某个 service 在配置文件被修改时，自动执行 reload / restart&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# （Salt 会直接调用 restart，除非设置 reload: True）&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;ntpd&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;service.running&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;watch&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/etc/ntp.conf&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;file.managed&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/etc/ntp.conf&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;salt://ntp/files/ntp.conf&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# 如何同时 watch 多个 state 呢？&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;nginx&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;service.running&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;reload&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;True&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;watch&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;foo&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;bar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;prereq&quot;&gt;prereq&lt;/h5&gt;

&lt;p&gt;被要求在 xxx state 之前执行。
它是以 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test=True&lt;/code&gt; 模式尝试去运行 xxx state，看是否有 “changes”。如果发现 xxx state 将会导致变化，那么立即赶在 xxx 之前执行当前 state。&lt;/p&gt;

&lt;h5 id=&quot;onfail&quot;&gt;onfail&lt;/h5&gt;

&lt;p&gt;另一个 state 执行失败了，执行这一个。&lt;/p&gt;

&lt;h5 id=&quot;onchanges&quot;&gt;onchanges&lt;/h5&gt;

&lt;p&gt;另一个 state 执行成功（result=True），并且产生变化。&lt;/p&gt;

&lt;h5 id=&quot;use&quot;&gt;use&lt;/h5&gt;

&lt;p&gt;可以复用另一个 state 的参数。&lt;/p&gt;

&lt;h5 id=&quot;unless&quot;&gt;unless&lt;/h5&gt;

&lt;p&gt;执行下面的内容，如果结果为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;False&lt;/code&gt;，才执行该 state。&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;enable_site&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;cmd.run&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;a2ensite tc-upgrade-server&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;unless&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;test -e /etc/apache2/sites-enabled/tc-upgrade-server&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;vim&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;pkg.installed&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;unless&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rpm -q vim-enhanced&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ls /usr/bin/vim&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;onlyif&quot;&gt;onlyif&lt;/h5&gt;

&lt;p&gt;执行下面的内容，如果结果为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;True&lt;/code&gt;，才执行该 state。&lt;/p&gt;

&lt;h5 id=&quot;listen--listen_in&quot;&gt;listen / listen_in&lt;/h5&gt;

&lt;p&gt;跟 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;watch / watch_in&lt;/code&gt; 相似，不过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listen / listen_in&lt;/code&gt; 不会改变其它 state 原来的执行顺序。
salt 会保证 &lt;strong&gt;所有&lt;/strong&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;listen / listen_in&lt;/code&gt; 的动作在 &lt;strong&gt;所有&lt;/strong&gt; state 的最后才被执行。&lt;/p&gt;

&lt;h5 id=&quot;check_cmd&quot;&gt;check_cmd&lt;/h5&gt;

&lt;p&gt;用于检查 state 是否按预期成功执行。可以人工指定执行成功的标准。有了它，对于一些执行结果有 error，但实际上我们认为是执行正确的情况，我们就可以直接重新定义检查标准了。如果改成 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/bin/true&lt;/code&gt; 那这个 sls 就永远执行成功。&lt;/p&gt;

&lt;h5 id=&quot;order&quot;&gt;order&lt;/h5&gt;

&lt;p&gt;可以指定第几个执行。也可指定它最后一个执行。&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;vim&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;pkg.installed&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;last&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;include&quot;&gt;include:&lt;/h4&gt;

&lt;p&gt;通过它可以组合利用多个 State。
Salt State 允许交叉引用，并会递归对需要的 State 进行引用。
&lt;strong&gt;include 了，就意味着这个 state 将会在这里执行一次。&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ssh&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;sshd&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;service.running&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;pkg&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;something_in_ssh...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;extend&quot;&gt;extend:&lt;/h4&gt;

&lt;p&gt;include 后，对之前的内容扩展。
对 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;require&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;watch&lt;/code&gt; 执行的是 append，不会覆盖老的。
而对其它属性，执行的是覆盖操作。&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ssh.server&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;extend&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;/etc/ssh/banner&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;salt://ssh/custom-banner&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;short-cuts&quot;&gt;Short cuts&lt;/h4&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file.managed&lt;/code&gt; 与下面的内容等价。相当于是下面的一种 short cuts。&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;managed&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;jinja2&quot;&gt;jinja2&lt;/h2&gt;

&lt;p&gt;Salt 默认用 jinja2 作为 rederer。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;基本使用：
http://docs.saltstack.com/en/latest/ref/renderers/all/salt.renderers.jinja.html
http://jinja.pocoo.org/docs
jinja2 内置的 filters http://jinja.pocoo.org/docs/dev/templates/#builtin-filters&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;在 State 里可以用 jinja 的一些语法来动态生成 State，可以用：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;salt&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grains&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pillar&lt;/code&gt;。
把 jinja、salt modules、python 语法相结合可以组合出很多巧妙的设计。&lt;/p&gt;

&lt;p&gt;除了 jinja 常规的语法，所有 salt 的 &lt;a href=&quot;http://docs.saltstack.com/en/latest/ref/modules/all/index.html&quot;&gt;builtin execution modules&lt;/a&gt; (像是那些 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd.run&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test.ping&lt;/code&gt;, 最常用的比如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grains.filter_by&lt;/code&gt;) 都是可以在 jinja 中通过以下形式来调用：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;salt['xxx_module.xxx_func'](*args, **kwargs)
或
salt.xxx_module.xxx_func(*args, **kwargs)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pillar.get()&lt;/code&gt; vs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;salt['pillar.get']()&lt;/code&gt;&lt;/p&gt;

  &lt;p&gt;It should be noted that within templating, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pillar&lt;/code&gt; variable is just a dictionary. This means that calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pillar.get()&lt;/code&gt; inside of a template will just use the default dictionary &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.get()&lt;/code&gt; function which does not include the extra &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt; delimiter functionality. It must be called using the above syntax (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;salt['pillar.get']('foo:bar:baz', 'qux')&lt;/code&gt;) to get the salt function, instead of the default dictionary behavior.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;注意到，用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt; 可以快速地找 dict 中的值，注意参数要加 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;''&lt;/code&gt; 引号。&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-jinja highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
# /srv/pillar/apache.sls
apache:
  lookup:
    name: httpd
    config:
      tmpl: salt://apache/files/httpd.conf

# /srv/salt/apache/conf.sls
&lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;apache/map.jinja&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;apache&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;context&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;
apache_conf:
  file:
    - managed
    - name: &lt;span class=&quot;cp&quot;&gt;{{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;salt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'pillar.get'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'apache:lookup:name'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;}}&lt;/span&gt;
    - source: &lt;span class=&quot;cp&quot;&gt;{{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;salt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'pillar.get'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'apache:lookup:config:tmpl'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;}}&lt;/span&gt;
    - ...

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其它例子：&lt;/p&gt;

&lt;div class=&quot;language-jinja highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;mnt&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;salt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'cmd.run'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'ls /dev/data/moose*'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;
/mnt/moose&lt;span class=&quot;cp&quot;&gt;{{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;mnt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;}}&lt;/span&gt;:
  mount.mounted:
    - device: &lt;span class=&quot;cp&quot;&gt;{{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;mnt&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;}}&lt;/span&gt;
    - fstype: xfs
    - mkmnt: True
  file.directory:
    - user: mfs
    - group: mfs
    - require:
      - user: mfs
      - group: mfs
&lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;endfor&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-jinja highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
apache:
  pkg.installed:
    &lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;grains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'os'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'RedHat'&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;
    - name: httpd
    &lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;endif&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;常用方式&quot;&gt;常用方式：&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;取字典中的值&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-jinja highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
# 两种都可以
&lt;span class=&quot;cp&quot;&gt;{{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;foo.bar&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;}}&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;{{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'bar'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;}}&lt;/span&gt;

# 取值，如果为 None，用默认值
&lt;span class=&quot;cp&quot;&gt;{{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;foo.bar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;| &lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;default&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'zoo'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;}}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;与 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file.managed&lt;/code&gt; 合用，用来渲染文件，替换文件中的变量&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-jinja highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
# redis.sls
/etc/redis/redis.conf:
    file.managed:
        - source: salt://redis.conf
        - template: jinja
        - context:
            redis: &lt;span class=&quot;cp&quot;&gt;{{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;redis&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;}}&lt;/span&gt;
            bind: 127.0.0.1

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;jinja 另一种经常出现的妙用是：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map.jinja&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map.jinja&lt;/code&gt; 放在 formula 中，通常为 state 提供变量。它可以整合 grains、默认值以及用户自定义的 pillar，为 state 提供数据。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;一些最佳实践：
http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html#writing-formulas
http://docs.saltstack.com/en/latest/topics/best_practices.html#variable-flexibility&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;运行-sls&quot;&gt;运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SLS&lt;/code&gt;&lt;/h2&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Run the whole formula (salt-tree)&lt;/span&gt;
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; state.highstate

&lt;span class=&quot;c&quot;&gt;# Run directly on minion&lt;/span&gt;
salt-call state.highstate &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; debug

&lt;span class=&quot;c&quot;&gt;# Run state testing&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# 这里的测试只是测：如果 State 运行，其第一步会运行什么。不是测整个 State 的过程。这在 State 内互相依赖条件较多时能看出。&lt;/span&gt;
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; state.sls &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;True
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; state.highstate &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;True
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; state.single &lt;span class=&quot;nb&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;True
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当 master 运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state.highstate&lt;/code&gt;，minion 会先下载 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;top.sls&lt;/code&gt;，如果发现有符合它的匹配，就把相应的文件下载、编译并运行。运行完成后，minion 会返回结果的汇总。&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/salt-3-state&quot;&gt;Salt (3) 核心所在：State&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on June 20, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Salt (2) 数据：Grains & Pillar]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/salt-2-grains-and-pillar" />
  <id>http://ohmystack.com/articles/salt-2-grains-and-pillar</id>
  <published>2015-05-30T00:00:00+08:00</published>
  <updated>2015-05-30T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;&lt;a href=&quot;/articles/salt-1-basic&quot;&gt;上一篇 Salt (1) 入门&lt;/a&gt; 介绍到：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Grains&lt;/code&gt; 是每一台 Minion 自身的 &lt;strong&gt;静态&lt;/strong&gt; 属性。以 Python 字典的形式存放在 Minion 端。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pillar&lt;/code&gt; 存放 key-value 变量。存放在 Master 端，由 Master 编译好后，下发给 Minion。所以，可以存放密码之类的涉密的或是一些需要统一配置的变量。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这篇文章，就来详细说说 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Grains&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pillar&lt;/code&gt; 。&lt;/p&gt;

&lt;h2 id=&quot;yaml-语法&quot;&gt;YAML 语法&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;http://docs.saltstack.com/en/latest/topics/yaml/index.html&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;在介绍 Grains 和 Pillar 之前呢，我们先来简单介绍一下 YAML 这种语法，因为我们自定义的 Grains、Pillar 以及下一篇要介绍的 States 都要用 YAML 语法。可以说，在 salt 中 YAML 无处不在。YAML 其实跟 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;json&lt;/code&gt; 类似，但是比 json 简洁许多，现在越来越多的地方也开始用 YAML 了。如果已经对 YAML 熟悉的朋友，可以跳过这第一部分。&lt;/p&gt;

&lt;p&gt;理解这种数据表示方式，很简单：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;注意缩进&lt;/li&gt;
  &lt;li&gt;key:value 用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt; （冒号）&lt;/li&gt;
  &lt;li&gt;list 用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt; （短横线）&lt;/li&gt;
  &lt;li&gt;注释用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#&lt;/code&gt; （井号）&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;例子：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# ========&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# SAMPLE 1&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# ========&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# in Python&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;my_key'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;my_value'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# in yaml&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;my_key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s&quot;&gt;my_value&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# ========&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# SAMPLE 2&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# ========&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# in Python&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;first_level_dict_key'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;second_level_dict_key'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;value_in_second_level_dict'&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# in yaml&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;first_level_dict_key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;second_level_dict_key&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;value_in_second_level_dict&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# ========&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# SAMPLE 3&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# ========&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# in Python&lt;/span&gt;
&lt;span class=&quot;pi&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;my_dictionary'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;list_value_one'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;list_value_two'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;list_value_three'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;]}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# in yaml&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;my_dictionary&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;list_value_one&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;list_value_two&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;list_value_three&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;grains---minion-自身的一些静态信息&quot;&gt;Grains - minion 自身的一些静态信息&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;http://docs.saltstack.com/en/latest/topics/targeting/grains.html&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; grains.ls       &lt;span class=&quot;c&quot;&gt;# 查看 grains 分类&lt;/span&gt;
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; grains.items    &lt;span class=&quot;c&quot;&gt;# 查看 grains 所有信息&lt;/span&gt;
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; grains.item os  &lt;span class=&quot;c&quot;&gt;# 查看 grains 某个信息&lt;/span&gt;
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; grains.get os
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;默认的这些，称为 &lt;em&gt;core grains&lt;/em&gt;。&lt;/p&gt;

&lt;p&gt;可以在 minion 的以下地方自定义 grains，如果重复定义，则会被依次覆盖。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Core grains.&lt;/li&gt;
  &lt;li&gt;Custom grain modules in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_grains&lt;/code&gt; directory, synced to minions.&lt;/li&gt;
  &lt;li&gt;Custom grains in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/grains&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Custom grains in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/minion&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;注意：&lt;/strong&gt; grains 一旦设定，就可以认为 minion 是将这个值存入了一个 python dict 中，并且这个值不会再变化了。这一点在当你写自定义的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_grains&lt;/code&gt; 时尤其要注意，你写的函数实际上只会被调用一次，而不是每次获取 grains 都去调用。如果想刷新 grains，只有通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;saltutil.sync_grains&lt;/code&gt; 或者 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;saltutil.sync_all&lt;/code&gt; 。&lt;/p&gt;

  &lt;p&gt;这也正是我们强调 grains 是 &lt;strong&gt;静态的&lt;/strong&gt; 的原因。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;刷新 Grains&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; saltutil.sync_grains
&lt;span class=&quot;c&quot;&gt;# OR&lt;/span&gt;
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; saltutil.sync_all
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;pillar---用来提供敏感的全局静态数据&quot;&gt;Pillar - 用来提供（敏感的）全局静态数据&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;http://docs.saltstack.com/en/latest/topics/pillar/index.html
http://docs.saltstack.com/en/latest/topics/best_practices.html#structuring-pillar-files&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;Pillar 以树状结构定义在 Salt Master 上。默认位置：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/srv/pillar&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;目录里面是由很多 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sls&lt;/code&gt; 文件组成。由 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;top.sls&lt;/code&gt; 统管。&lt;/li&gt;
  &lt;li&gt;Pillar 可以存放结构化数据。key/value, list …&lt;/li&gt;
  &lt;li&gt;Pillar 里面的数据可以存一些敏感信息。它们会被安全送达相关的 minion。&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;Pillar 与 Grains 简单区别：&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;Grains 是 minion 自己生成、自己保存的信息。&lt;/li&gt;
    &lt;li&gt;Pillar 是 Salt Master 下发的信息。&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;pillar-简单示例&quot;&gt;Pillar 简单示例&lt;/h4&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 查看 minion 的 pillar 数据&lt;/span&gt;
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; pillar.items
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; pillar.get xxx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;pillar-配置&quot;&gt;Pillar 配置&lt;/h4&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# /srv/pillar/top.sls:&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# 说明所有 minions 可以访问 data.sls 里的数据&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;*'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;pillar-的内容&quot;&gt;Pillar 的内容&lt;/h4&gt;

&lt;p&gt;它就是一些 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.sls&lt;/code&gt; 格式的 YAML 文件。关于 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.sls&lt;/code&gt; 是什么，我们会在下一篇讲到。
比如，我来写一个 pillar，存放一些 git 相关的变量：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# /srv/pillar/global.sls&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;git&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; 
  &lt;span class=&quot;na&quot;&gt;username&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ohmystack&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;jiangjun1990@gmail.com&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;clone_to_home_dir&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;Dev&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;#####如果两个 pillar 的 key 重复了，会怎样？
呵呵。Remember: conflicting keys will be overwritten in a non-deterministic manner!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;pillar-与-state-结合&quot;&gt;Pillar 与 State 结合&lt;/h4&gt;

&lt;p&gt;可以在一个 pillar 目录下，放一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init.sls&lt;/code&gt;，只要 include 这层目录，就能把这个 pillar 的数据导入。&lt;/p&gt;

&lt;p&gt;例如：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# /srv/pillar/users/init.sls:&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;users&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;thatch&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1000&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;shouse&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1001&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;utahdave&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1002&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;redbeard&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1003&lt;/span&gt;
  
&lt;span class=&quot;c1&quot;&gt;# /srv/pillar/top.sls:&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;s1&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;*'&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;data&lt;/span&gt;
    &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;users&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后在 State 里结合 pillar&lt;/p&gt;

&lt;div class=&quot;language-jinja highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
# /srv/salt/users/init.sls
&lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;uid&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pillar.get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'users'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;{}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;items&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;{{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;}}&lt;/span&gt;:
  user.present:
    - uid: &lt;span class=&quot;cp&quot;&gt;{{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;uid&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;}}&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;endfor&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;为不同情况定制-pillar-data&quot;&gt;为不同情况定制 pillar data&lt;/h4&gt;

&lt;div class=&quot;language-jinja highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
#/srv/pillar/pkg/init.sls:
pkgs:
  &lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;grains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'os_family'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'RedHat'&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;
  apache: httpd
  vim: vim-enhanced
  &lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;grains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'os_family'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'Debian'&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;
  apache: apache2
  vim: vim
  &lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;grains&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'os'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'Arch'&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;
  apache: apache
  vim: vim
  &lt;span class=&quot;cp&quot;&gt;{%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;endif&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%}&lt;/span&gt;
  
#/srv/salt/apache/init.sls:
apache:
  pkg.installed:
    - name: &lt;span class=&quot;cp&quot;&gt;{{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;pillar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'pkgs'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;][&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'apache'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;}}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;为-pillar-提供默认值&quot;&gt;为 pillar 提供默认值&lt;/h4&gt;

&lt;p&gt;Pillar 目前来说，就是 Python 里的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dict&lt;/code&gt; 。可以用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get&lt;/code&gt; 、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;items&lt;/code&gt; …&lt;/p&gt;

&lt;div class=&quot;language-jinja highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
apache:
  pkg.installed:
    - name: &lt;span class=&quot;cp&quot;&gt;{{&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;salt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'pillar.get'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'pkgs:apache'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'httpd'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;}}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;刷新-pillar&quot;&gt;刷新 pillar&lt;/h4&gt;

&lt;p&gt;修改 pillar 后，可以用下面命令立即同步，下发到 minion 上。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; saltutil.refresh_pillar
&lt;span class=&quot;c&quot;&gt;# OR&lt;/span&gt;
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; saltutil.sync_all
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;进阶pillar-还可以用来干什么&quot;&gt;进阶：Pillar 还可以用来干什么？&lt;/h4&gt;

&lt;p&gt;它还可以和 Salt 的 environment 功能结合，为指定范围的 minion 赋予 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;role&lt;/code&gt; 的概念。（其实随便叫什么都可以，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;role&lt;/code&gt; 只是普普通通的一个 key 而已。）&lt;/p&gt;

&lt;blockquote&gt;
  &lt;h5 id=&quot;详见&quot;&gt;详见：&lt;/h5&gt;
  &lt;p&gt;http://docs.saltstack.com/en/latest/topics/tutorials/states_pt4.html
http://docs.saltstack.com/en/latest/topics/best_practices.html#structuring-pillar-files&lt;/p&gt;
&lt;/blockquote&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/salt-2-grains-and-pillar&quot;&gt;Salt (2) 数据：Grains & Pillar&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on May 30, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[用终端下载 Youtube 视频]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/download-youtube-in-terminal" />
  <id>http://ohmystack.com/articles/download-youtube-in-terminal</id>
  <published>2015-02-23T00:00:00+08:00</published>
  <updated>2015-02-23T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;安装 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;youtube-dl&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 请用 pip 安装，这样 youtube-dl 的版本可以保证比较新&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;pip &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;youtube-dl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;（使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;youtube-dl&lt;/code&gt; 并不代表不需要翻墙）&lt;/p&gt;

&lt;p&gt;如果需要使用 HTTP proxy，只要像往常一样&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HTTP_PROXY&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'http://&amp;lt;proxy_id&amp;gt;:&amp;lt;port&amp;gt;'&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;HTTPS_PROXY&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'http://&amp;lt;proxy_id&amp;gt;:&amp;lt;port&amp;gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;让我们简单看下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;youtube-dl&lt;/code&gt; 其中几个重要参数&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;man youtube-dl

&lt;span class=&quot;c&quot;&gt;# Video Format Options:&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#            -f, --format FORMAT              video format code, specify the order of&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#                                             preference using slashes: &quot;-f 22/17/18&quot;.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#                                             &quot;-f mp4&quot; and &quot;-f flv&quot; are also supported.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#                                             You can also use the special names &quot;best&quot;,&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#                                             &quot;bestaudio&quot;, &quot;worst&quot;, and &quot;worstaudio&quot;. By&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#                                             default, youtube-dl will pick the best&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#                                             quality.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#            --all-formats                    download all available video formats&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#            --prefer-free-formats            prefer free video formats unless a specific&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#                                             one is requested&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#            --max-quality FORMAT             highest quality format to download&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;#            -F, --list-formats               list all available formats&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;假设我想下载 https://www.youtube.com/watch?v=bBeFZgEYYI8 这个视频&lt;/p&gt;

&lt;h3 id=&quot;step1&quot;&gt;Step1.&lt;/h3&gt;

&lt;p&gt;用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-F&lt;/code&gt; 列出这一个视频有哪些格式 &amp;amp; 分辨率的组合可供下载&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;youtube-dl https://www.youtube.com/watch?v&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;bBeFZgEYYI8 &lt;span class=&quot;nt&quot;&gt;-F&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;youtube] Setting language
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;youtube] bBeFZgEYYI8: Downloading webpage
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;youtube] bBeFZgEYYI8: Downloading video info webpage
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;youtube] bBeFZgEYYI8: Extracting video information
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;info] Available formats &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;bBeFZgEYYI8:
format code extension resolution  note
171         webm      audio only  DASH webm audio , audio@ 48k &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;worst&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
140         m4a       audio only  DASH audio , audio@128k
160         mp4       192p        DASH video
242         webm      240p        DASH webm
133         mp4       240p        DASH video
243         webm      360p        DASH webm
134         mp4       360p        DASH video
244         webm      480p        DASH webm
135         mp4       480p        DASH video
247         webm      720p        DASH webm
136         mp4       720p        DASH video
248         webm      1080p       DASH webm
137         mp4       1080p       DASH video
17          3gp       176x144
36          3gp       320x240
5           flv       400x240
43          webm      640x360
18          mp4       640x360
22          mp4       1280x720    &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;best&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;step2&quot;&gt;Step2.&lt;/h3&gt;

&lt;p&gt;如果要下载 720p mp4 的视频，我们找到它的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;format code&lt;/code&gt; 为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;136&lt;/code&gt;，用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-f 136&lt;/code&gt; 下载&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;youtube-dl https://www.youtube.com/watch?v&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;bBeFZgEYYI8 &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; 136
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这样视频就下载下来了。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;NOTE:&lt;br /&gt;
最好不要去死记这个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;format code&lt;/code&gt;，因为 Youtube 不一定为每个视频都准备齐这么多格式组合。
我们最好还是在下载前，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-F&lt;/code&gt; 探测一下格式有哪些。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h5 id=&quot;一点花絮&quot;&gt;一点花絮&lt;/h5&gt;

&lt;p&gt;事出有因，大过年的，我 Linode Tokyo 节点的 IP 被限速了。
好在似乎只是被家这边的电信限速，用公司的 VPN （换成上海的 IP）过去速度还是不错的。
所以我就想到把视频下载到墙外的 VM，然后用公司 VPN 传回来。&lt;/p&gt;

&lt;p&gt;发现在 Linode 下载 Youtube 视频的速度真是杠杠的。&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/download-youtube-in-terminal&quot;&gt;用终端下载 Youtube 视频&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on February 23, 2015.&lt;/p&gt;</content>
</entry>


<entry>
  <title type="html"><![CDATA[Salt (1) 入门]]></title>
  <link rel="alternate" type="text/html" href="http://ohmystack.com/articles/salt-1-basic" />
  <id>http://ohmystack.com/articles/salt-1-basic</id>
  <published>2014-11-20T00:00:00+08:00</published>
  <updated>2014-11-20T00:00:00+08:00</updated>
  <author>
    <name>JunJiang江骏</name>
    <uri>http://ohmystack.com</uri>
    <email>jiangjun1990@gmail.com</email>
  </author>
  <content type="html">&lt;p&gt;&lt;a href=&quot;http://docs.saltstack.com/en/latest/&quot;&gt;Salt&lt;/a&gt; 是一个统一配置管理工具（与 puppet 功能相似）。可以批量远程执行命令、修改配置文件、部署软件、定时执行任务…… 总之，你能想到的，从远程对服务器执行的操作，它几乎都能做，而且可以批量对一批服务器做。&lt;/p&gt;

&lt;p&gt;Salt 采用 master(server) - minion(client) 模式。具有很好的水平拓展性，以 “支持无限多个 client” 为设计目标。&lt;/p&gt;

&lt;h2 id=&quot;总体架构&quot;&gt;总体架构&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts_img/2014-11-20-salt_architecture.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Salt Master&lt;/code&gt; 控制中心。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Salt Minion&lt;/code&gt; 装在被操作的服务器上。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Grains&lt;/code&gt; 是每一台 Minion 自身的 &lt;strong&gt;静态&lt;/strong&gt; 属性。以 Python 字典的形式存放在 Minion 端。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pillar&lt;/code&gt; 存放 key-value 变量。存放在 Master 端，由 Master 编译好后，下发给 Minion。所以，可以存放密码之类的涉密的或是一些需要统一配置的变量。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;State&lt;/code&gt; 希望由 Salt 执行的一套操作。（比如 “安装 vim、配置 vim” 可以写在一个 state 里，也可以拆成两个 state 写。）执行时，Master 将 states 生成好，下发给 Minion，由 Minion 执行 states （转化成一条条命令）。&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;安装-salt&quot;&gt;安装 salt&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;各种安装方法：&lt;br /&gt;
http://docs.saltstack.com/en/latest/topics/tutorials/salt_bootstrap.html&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;通过包管理器安装&quot;&gt;通过包管理器安装&lt;/h4&gt;

&lt;p&gt;http://docs.saltstack.com/en/latest/topics/installation/ubuntu.html&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;add-apt-repository ppa:saltstack/salt
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get update
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;salt-master salt-minion salt-ssh salt-syndic
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;或者&lt;/p&gt;

&lt;h4 id=&quot;源码安装从某个-git-tag-安装&quot;&gt;源码安装（从某个 &lt;a href=&quot;https://github.com/saltstack/salt/releases&quot;&gt;git tag&lt;/a&gt; 安装）&lt;/h4&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; install_salt.sh &lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt; https://bootstrap.saltstack.com
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;sh install_salt.sh git v0.16.4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;salt-端口&quot;&gt;Salt 端口&lt;/h2&gt;

&lt;p&gt;TCP 4505 &amp;amp; 4506&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;基本配置&quot;&gt;基本配置&lt;/h2&gt;

&lt;p&gt;都在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt&lt;/code&gt; 下，&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/etc/salt/master
/etc/salt/minion
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;master-配置&quot;&gt;Master 配置&lt;/h4&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href=&quot;http://salt.readthedocs.org/en/latest/ref/configuration/master.html&quot;&gt;master configuration reference&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;每次更新，需要重启 Salt Master。&lt;br /&gt;
Master 监听 0.0.0.0:4505 和 4506&lt;/p&gt;

&lt;h4 id=&quot;minion-配置&quot;&gt;Minion 配置&lt;/h4&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;a href=&quot;http://salt.readthedocs.org/en/latest/ref/configuration/minion.html&quot;&gt;minion configuration reference&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;每次更新，需要重启 Salt Minion。&lt;br /&gt;
通常都需要在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/minion&lt;/code&gt; 配置 master 这一项，指定其 Salt Master。&lt;br /&gt;
如果不配置这一项，Minion 会自动通过 DNS 尝试连接 hostname 为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;salt&lt;/code&gt; 的这个服务器。&lt;/p&gt;

&lt;h4 id=&quot;utf-8&quot;&gt;utf-8&lt;/h4&gt;

&lt;p&gt;解决在 sls 文件中使用中文的问题，在 master 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/master&lt;/code&gt; &amp;amp; minion 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/minion&lt;/code&gt; 上都配置&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;yaml_utf8: True
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;key-的配置&quot;&gt;Key 的配置&lt;/h4&gt;

&lt;p&gt;Salt 使用 AES 加密 Master 与 Minion 之间通讯。而如果要想让 Master 认识一个 Minion，这个 Master 就必须接受这个 Minion 的 key。&lt;/p&gt;

&lt;p&gt;重启 salt-minion，会触发 Minion 把自己的 key 发给 Master。&lt;/p&gt;

&lt;p&gt;在 Salt Master 上：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;salt-key &lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# list all the keys&lt;/span&gt;
salt-key &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# accept all the keys&lt;/span&gt;
salt-key &lt;span class=&quot;nt&quot;&gt;-D&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# delete all the keys&lt;/span&gt;
salt-key &lt;span class=&quot;nt&quot;&gt;-R&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# reject all pending keys&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果只想对某个 key 进行操作，只需把参数的大写字母换成小写字母。&lt;br /&gt;
例如：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;salt-key &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; &amp;lt;keyname&amp;gt;  &lt;span class=&quot;c&quot;&gt;# accept a specific key&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;h4 id=&quot;minion_id-是如何生成的&quot;&gt;&lt;strong&gt;minion_id 是如何生成的？&lt;/strong&gt;&lt;/h4&gt;

  &lt;p&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/minion&lt;/code&gt; 中的 id 优先级最高。&lt;/strong&gt;如果没有设置，Minion 会再按以下顺序寻找值来作为 minion_id：&lt;/p&gt;

  &lt;ol&gt;
    &lt;li&gt;Python 函数 socket.getfqdn() 获取的值&lt;/li&gt;
    &lt;li&gt;/etc/hostname 定义的值(仅限于非 Windows 系统)&lt;/li&gt;
    &lt;li&gt;/etc/hosts (%WINDIR%\system32\drivers\etc\hosts on Windows hosts) 中定义的任何映射 127.0.0.0/8 的主机名做 ID 值&lt;/li&gt;
  &lt;/ol&gt;

  &lt;p&gt;最终获取的 ID 将记录在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/minion_id&lt;/code&gt; 文件中，该文件可以手动更改，重启服务后不会被重新覆盖。
但如果 minion 的配置文件 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/minion&lt;/code&gt; 中设置了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;id: xxxx&lt;/code&gt;，那么这个 id 值将取代 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/salt/minion_id&lt;/code&gt; 中记录的 ID 数值。&lt;/p&gt;

  &lt;p&gt;http://docs.saltstack.com/en/latest/topics/tutorials/walkthrough.html#minion-id-generation&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;文件位置&quot;&gt;文件位置&lt;/h2&gt;

&lt;p&gt;Salt 的文件分为 &lt;strong&gt;State 文件&lt;/strong&gt; 和 &lt;strong&gt;Pillar 文件&lt;/strong&gt; 。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;State 文件&lt;/strong&gt; 默认存放在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/srv/salt&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Pillar 文件&lt;/strong&gt; 默认存放在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/srv/pillar&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这两个路径都可以通过 /etc/salt/master 的 fileserver_backend, file_roots, pillar_roots 进行修改。一些高级的，跟环境 (env) 的设置，后面再介绍。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;基本命令&quot;&gt;基本命令&lt;/h2&gt;

&lt;h4 id=&quot;启动-salt&quot;&gt;启动 Salt&lt;/h4&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 根据自己需要，选择以下启动方式&lt;/span&gt;
salt-master
salt-master &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# daemonize the process&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;service salt-master start  &lt;span class=&quot;c&quot;&gt;# linux service&lt;/span&gt;
salt-master &lt;span class=&quot;nt&quot;&gt;--log-level&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;debug

salt-minion
salt-minion &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# daemonize the process&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;service salt-minion start  &lt;span class=&quot;c&quot;&gt;# linux service&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;执行-salt-命令&quot;&gt;执行 Salt 命令&lt;/h4&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;salt '&amp;lt;target&amp;gt;' &amp;lt;function&amp;gt; [arguments]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;target&lt;/strong&gt;:&lt;br /&gt;
用来指定对哪些 minion 执行操作。可以匹配 minion id 或 Grains。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 支持正则表达、列表以及 id/grains 混合。&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# 普通 (按 minion-id 过滤)&lt;/span&gt;
salt &lt;span class=&quot;s1&quot;&gt;'*.example.org'&lt;/span&gt; test.ping

&lt;span class=&quot;c&quot;&gt;# 正则表达 (regular expression)&lt;/span&gt;
salt &lt;span class=&quot;nt&quot;&gt;-E&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'virtmach[0-9]'&lt;/span&gt; test.ping

&lt;span class=&quot;c&quot;&gt;# 列表 (list)&lt;/span&gt;
salt &lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'foo,bar,baz,quo'&lt;/span&gt; test.ping

&lt;span class=&quot;c&quot;&gt;# 混合型 (combined)&lt;/span&gt;
salt &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'G@os:Ubuntu and webser* or E@database.*'&lt;/span&gt; test.ping
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;| Letter |  Match Type        | Example                             |
| :----- | :----------------- | :---------------------------------- |
| G      | Grains glob        | G@os:Ubuntu                         |
| E      | 正则匹配 Minion ID  | E@web\d+\.(dev\|qa\|prod)\.loc      |
| P      | Grains 正则匹配     | P@os:(RedHat\|Fedora\|CentOS)       |
| L      | List of minions    | L@minion1,minion3 or bl*.domain.com |
| I      | Pillar glob        | I@pdata:foobar                      |
| S      | Subnet/IP address  | S@192.168.1.0/24 or S@192.168.1.100 |
| R      | Range cluster      | R@%foo.bar                          |
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;function&lt;/strong&gt;:&lt;br /&gt;
指定执行哪个命令模块(module)。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;i class=&quot;fa fa-exclamation-circle&quot;&gt;&lt;/i&gt; 要习惯从下面这个 salt builtin execution modules 列表中寻找自己需要的，点进去看详细的命令参数。&lt;br /&gt;
http://docs.saltstack.com/en/latest/ref/modules/all/&lt;/p&gt;

  &lt;p&gt;常用的举例：&lt;/p&gt;

  &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; test.ping
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; cmd.run &lt;span class=&quot;s2&quot;&gt;&quot;ls -l | awk '/foo/{print &lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\$&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2}'&quot;&lt;/span&gt;
salt &lt;span class=&quot;s1&quot;&gt;'*'&lt;/span&gt; state.sls xxx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;arguments&lt;/strong&gt;:&lt;br /&gt;
module 的参数。通过空格分隔，加在 function 后面。
比如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-l debug&lt;/code&gt; 输出 debug 信息&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;salt 命令参数：http://docs.saltstack.com/en/latest/ref/cli/salt.html&lt;br /&gt;
salt module 使用指南：http://docs.saltstack.cn/topics/tutorials/modules.html&lt;br /&gt;
salt builtin execution modules 列表： http://docs.saltstack.com/en/latest/ref/modules/all/&lt;br /&gt;
salt logging 指南：http://salt.readthedocs.org/en/latest/ref/configuration/logging/index.html&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Q&amp;amp;A:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;h4 id=&quot;是否可以在-salt-minion-上直接无-master-运行-modulestate-&quot;&gt;&lt;strong&gt;是否可以在 salt minion 上直接无 master 运行 module/state ？&lt;/strong&gt;&lt;/h4&gt;

  &lt;p&gt;答案所肯定的。首先，这需要把 /etc/salt/minion 像 /etc/salt/master 那样配置好：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file_roots&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pillar_roots&lt;/code&gt;，然后：&lt;/p&gt;

  &lt;h5 id=&quot;方法一&quot;&gt;方法一：&lt;/h5&gt;

  &lt;p&gt;本来 salt 会去 master 上找 file server 和 pillar data，现在则需要把 /etc/salt/minion 里的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file_client&lt;/code&gt; 从默认的 remote 改为 local，这样 salt 就会在本地寻找。&lt;/p&gt;

  &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;file_client: local
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;运行&lt;/p&gt;

  &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;salt-call test.ping
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;h5 id=&quot;方法二&quot;&gt;方法二：&lt;/h5&gt;

  &lt;p&gt;直接运行&lt;/p&gt;

  &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;salt-call &lt;span class=&quot;nt&quot;&gt;--local&lt;/span&gt; test.ping
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--local&lt;/code&gt; 参数让 Minion 在本地寻找 salt tree，而不用修改 /etc/salt/minion&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;

&lt;p&gt;至此，Salt 入门完成了。你已经学会用 salt 自带的 modules，对选定范围的 minion 执行命令了。&lt;/p&gt;

  &lt;p&gt;&lt;a href=&quot;http://ohmystack.com/articles/salt-1-basic&quot;&gt;Salt (1) 入门&lt;/a&gt; was originally published by JunJiang江骏 at &lt;a href=&quot;http://ohmystack.com&quot;&gt;Oh My Stack&lt;/a&gt; on November 20, 2014.&lt;/p&gt;</content>
</entry>

</feed>
