OpenStack Neutronの「中」の動きを追いかけよういま覚えておくべきOpenStack Neutronの基本(2)(4/8 ページ)

» 2015年02月04日 19時00分 公開
[SDNエバンジェリスト吉本昌平ユニアデックス株式会社]

ここまでの内容を、Neutronはどうやって実行している?

おめでとう! ところで、Maiaちゃん、これって、何かに似てきていると思わない?



そう。あの記事のテナントの中の仮想ルーターとインスタンス


あぁ。だからそんな名前だったのか。ということは、あたしたち、いま第一回の記事で紹介したNeutronの挙動と同じことやろうとしてるということ??


その通り


すごーい!! でも、実際には何かAPI的なものがあるのよね?


実は僕もそう思ってNeutronのソースコードを見てみたんだ。さっき出てきたveth pairを作ってるのは、neutron/agent/linux/ip_lib.pyに記述があるadd_vethってところ。2行目のargsの中に、さっき叩いたのと同じコマンドが並んでいるのが分かるだろう?


ほんとだ。最後にlinkを付けてas_rootに渡しているから、後は先頭に「ip」が付けばいいのかな?


neutron/agent/linux/ip_lib.py
    def add_veth(self, name1, name2, namespace2=None):
        args = ['add', name1, 'type', 'veth', 'peer', 'name', name2]
        if namespace2 is None:
            namespace2 = self.namespace
        else:
            self.ensure_namespace(namespace2)
            args += ['netns', namespace2]
        self._as_root('', 'link', tuple(args))
        return (IPDevice(name1, self.root_helper, self.namespace),
                IPDevice(name2, self.root_helper, namespace2))

as_rootをたどっていくと、下のリストにあるように、「ip」を付けているのが分かるよ


neutron/agent/linux/ip_lib.py
    def _execute(cls, options, command, args, root_helper=None,
                 namespace=None):
        opt_list = ['-%s' % o for o in options]
        if namespace:
            ip_cmd = ['ip', 'netns', 'exec', namespace, 'ip']
        else:
            ip_cmd = ['ip']
        return utils.execute(ip_cmd + opt_list + [command] + list(args),
                             root_helper=root_helper)

そして、最後に渡された文字列で次のリストにあるようにcreate_processを実行しているんだ


neutron/agent/linux/utils.py
def execute(cmd, root_helper=None, process_input=None, addl_env=None,
            check_exit_code=True, return_stderr=False):
    try:
        obj, cmd = create_process(cmd, root_helper=root_helper,
                                  addl_env=addl_env)
        _stdout, _stderr = (process_input and
                           obj.communicate(process_input) or
                            obj.communicate())
        obj.stdin.close()
        m = _("\nCommand: %(cmd)s\nExit code: %(code)s\nStdout: %(stdout)r\n"
              "Stderr: %(stderr)r") % {'cmd': cmd, 'code': obj.returncode,
                                       'stdout': _stdout, 'stderr': _stderr}
        LOG.debug(m)
        if obj.returncode and check_exit_code:
            raise RuntimeError(m)
    finally:
        # NOTE(termie): this appears to be necessary to let the subprocess
        #               call clean something up in between calls, without
        #               it two execute calls in a row hangs the second one
        greenthread.sleep(0)
    return return_stderr and (_stdout, _stderr) or _stdout

Copyright © ITmedia, Inc. All Rights Reserved.

RSSについて

アイティメディアIDについて

メールマガジン登録

@ITのメールマガジンは、 もちろん、すべて無料です。ぜひメールマガジンをご購読ください。