Merge "Expose date time as facts"

This commit is contained in:
Zuul 2019-06-18 19:52:05 +00:00 committed by Gerrit Code Review
commit a7a2eaa74a
7 changed files with 105 additions and 3 deletions

View File

@ -0,0 +1,15 @@
- pipeline:
name: promote
manager: supercedent
post-review: true
trigger:
gerrit:
- event: change-merged
- job:
name: base
parent: null
nodeset:
nodes:
- name: ubuntu-xenial
label: ubuntu-xenial

View File

@ -0,0 +1 @@
test

View File

@ -0,0 +1,11 @@
- hosts: localhost
gather_facts: smart
tasks:
- debug:
var: date_time
- assert:
that: date_time is defined
- debug:
var: ansible_date_time
- assert:
that: ansible_date_time is not defined

View File

@ -0,0 +1,10 @@
- job:
parent: base
name: datetime-fact
run: playbooks/datetime-fact.yaml
- project:
name: org/project
promote:
jobs:
- datetime-fact

View File

@ -0,0 +1,8 @@
- tenant:
name: tenant-one
source:
gerrit:
config-projects:
- common-config
untrusted-projects:
- org/project

View File

@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import logging
import multiprocessing
import os
@ -707,3 +708,28 @@ class TestLineMapping(AnsibleZuulTestCase):
'name': 'Zuul',
'username': 'jenkins'}}
)
class TestExecutorFacts(AnsibleZuulTestCase):
tenant_config_file = 'config/executor-facts/main.yaml'
def _get_file(self, build, path):
p = os.path.join(build.jobdir.root, path)
with open(p) as f:
return f.read()
def test_datetime_fact(self):
self.executor_server.keep_jobdir = True
A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
self.fake_gerrit.addEvent(A.getChangeMergedEvent())
self.waitUntilSettled()
self.assertEqual(self.getJobFromHistory('datetime-fact').result,
'SUCCESS')
j = json.loads(self._get_file(self.history[0],
'work/logs/job-output.json'))
date_time = \
j[0]['plays'][0]['tasks'][0]['hosts']['localhost']['date_time']
self.assertEqual(18, len(date_time))

View File

@ -403,13 +403,44 @@ class JobDir(object):
self.job_unreachable_file = os.path.join(self.ansible_cache_root,
'nodes.unreachable')
os.makedirs(self.control_path)
localhost_facts = os.path.join(self.fact_cache, 'localhost')
jobtime = datetime.datetime.utcnow()
date_time_facts = {}
date_time_facts['year'] = jobtime.strftime('%Y')
date_time_facts['month'] = jobtime.strftime('%m')
date_time_facts['weekday'] = jobtime.strftime('%A')
date_time_facts['weekday_number'] = jobtime.strftime('%w')
date_time_facts['weeknumber'] = jobtime.strftime('%W')
date_time_facts['day'] = jobtime.strftime('%d')
date_time_facts['hour'] = jobtime.strftime('%H')
date_time_facts['minute'] = jobtime.strftime('%M')
date_time_facts['second'] = jobtime.strftime('%S')
date_time_facts['epoch'] = jobtime.strftime('%s')
date_time_facts['date'] = jobtime.strftime('%Y-%m-%d')
date_time_facts['time'] = jobtime.strftime('%H:%M:%S')
date_time_facts['iso8601_micro'] = \
jobtime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ")
date_time_facts['iso8601'] = \
jobtime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
date_time_facts['iso8601_basic'] = jobtime.strftime("%Y%m%dT%H%M%S%f")
date_time_facts['iso8601_basic_short'] = \
jobtime.strftime("%Y%m%dT%H%M%S")
# Set the TZ data manually as jobtime is naive.
date_time_facts['tz'] = 'UTC'
date_time_facts['tz_offset'] = '+0000'
executor_facts = {}
executor_facts['date_time'] = date_time_facts
executor_facts['module_setup'] = True
# NOTE(pabelanger): We do not want to leak zuul-executor facts to other
# playbooks now that smart fact gathering is enabled by default. We
# can have ansible skip populating the cache with information by the
# doing the following.
# can have ansible skip populating the cache with information by
# writing a file with the minimum facts we want.
with open(localhost_facts, 'w') as f:
f.write('{"module_setup": true}')
json.dump(executor_facts, f)
self.result_data_file = os.path.join(self.work_root, 'results.json')
with open(self.result_data_file, 'w'):