(一) 从代码看,携带以下信息:
安全上下文,请求信息;用来表示“正在执行一定动作的用户”
还包含db session
从类定义看所携带具体信息:
- class ContextBase(common_context.RequestContext):
- ...
- def to_dict(self):
- return {'user_id': self.user_id,
- 'tenant_id': self.tenant_id,
- 'project_id': self.project_id,
- 'is_admin': self.is_admin,
- 'read_deleted': self.read_deleted,
- 'roles': self.roles,
- 'timestamp': str(self.timestamp),
- 'request_id': self.request_id,
- 'tenant': self.tenant,
- 'user': self.user,
- 'tenant_name': self.tenant_name,
- 'project_name': self.tenant_name,
- 'user_name': self.user_name,
- }
- ...
-
- class Context(ContextBase):
- @property
- def session(self):
- if self._session is None:
- self._session = db_api.get_session()
- return self._session
(二) Neutron在哪里用context
所有neutron API,如create_network
def create_network(self, context, network):
(三) 抓一下context对象
1. 添加LOG
/opt/stack/neutron/neutron/plugins/ml2/plugin.py
- def create_network(self, context, network):
- LOG.info(_("**debug** %s, %s"), context.to_dict(), network)
2. devstack环境中,停掉neutron-server
- $ ps -eLf | grep neutron-server
- bengo 7884 2672 7884 0 1 18:35 pts/0 00:00:00 grep --color=auto neutron-server
- bengo 10246 2672 10246 0 1 17:04 pts/0 00:00:21 python /usr/local/bin/neutron-server --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugins/ml2/ml2_conf.ini
-
- $ sudo kill $pid
3. devstack中为配置log,配置/etc/neutron/neuron.conf
- debug = False
- use_stderr = False
- log_file = server.log
- log_dir = /var/log/neutron
4. 启动neutron-server
- $ python /usr/local/bin/neutron-server --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugins/ml2/ml2_conf.ini &
5. 在neutron-client创建network
$ neutron net-create test-04
log文件中:
- 2014-01-17 17:04:55.702 INFO neutron.plugins.ml2.plugin [req-87d27c81-c30c-43ea-baa1-dead845e9a2a demo e70c0a66ca274efc9ca32a5d0532c298] **debug** {'project_name': u'demo', 'tenant_name': u'demo', 'timestamp': '2014-01-17 09:04:55.614450', 'is_admin': False, 'user': u'bcfca75777ed447297dee807677ee99e', 'tenant': u'e70c0a66ca274efc9ca32a5d0532c298', 'user_id': u'bcfca75777ed447297dee807677ee99e', 'roles': [u'anotherrole', u'Member'], 'tenant_id': u'e70c0a66ca274efc9ca32a5d0532c298', 'read_deleted': 'no', 'request_id': 'req-87d27c81-c30c-43ea-baa1-dead845e9a2a', 'project_id': u'e70c0a66ca274efc9ca32a5d0532c298', 'user_name': u'demo'}, {u'network': {'router:external': <object object at 0x7f7d81508140>, u'name': u'test-04', 'provider:physical_network': <object object at 0x7f7d81508140>, u'admin_state_up': True, 'tenant_id': u'e70c0a66ca274efc9ca32a5d0532c298', 'segments': <object object at 0x7f7d81508140>, 'provider:network_type': <object object at 0x7f7d81508140>, 'shared': False, 'provider:segmentation_id': <object object at 0x7f7d81508140>}}
6. 解析、整形
(1) import json
json.dumps(xxx)
(2) 用 $ echo ‘xxx’ | python -mjson.tool解析
context:
- {
- "is_admin": false,
- "project_id": "e70c0a66ca274efc9ca32a5d0532c298",
- "project_name": "demo",
- "read_deleted": "no",
- "request_id": "req-5e88df50-e4ef-4ee2-be09-ee7d9ce25236",
- "roles": [
- "anotherrole",
- "Member"
- ],
- "tenant": "e70c0a66ca274efc9ca32a5d0532c298",
- "tenant_id": "e70c0a66ca274efc9ca32a5d0532c298",
- "tenant_name": "demo",
- "timestamp": "2014-01-18 01:20:38.758066",
- "user": "bcfca75777ed447297dee807677ee99e",
- "user_id": "bcfca75777ed447297dee807677ee99e",
- "user_name": "demo"
- }