2020年2月13日 星期四

[NodeJS 文章收集] NodeJS accessing file with relative path

Source From Here
Question
It seemed like a straight forward problem. But I am not able to crack this. Within helper1.js I would like to access foobar.json (from config/dev/)
  1. root  
  2.   -config  
  3.    --dev  
  4.     ---foobar.json  
  5.   -helpers  
  6.    --helper1.js  
Any help here would be great.

How-To
You can use the path module to join the path of the directory in which helper1.js lives to the relative path of foobar.json. This will give you the absolute path to foobar.json:
  1. var fs = require('fs');  
  2. var path = require('path');  
  3.   
  4. var jsonPath = path.join(__dirname, '..''config''dev''foobar.json');  
  5. var jsonString = fs.readFileSync(jsonPath, 'utf8');  

[Ansible 文章收集] How to check if a file/directory exists in Ansible

Source From Here
Preface
To get the details of a file or directory in a Linux system, we can use the Ansible stat module. It works similar to the Linux ‘stat’ command. Of course, the module provides much more detail than whether a file exists or not. We can know when the file was last modified, what all permissions the invoking user has about the file, whether it is a directory etc.

If you just need to check the status of the file to create a new file, then you should use the file module. It will create a file or directory only if the file does not exist. I will show you an example below. For more details on how to create directories in Ansible, you can refer this post.

The stat module needs the path of the file, directory or symlink to be checked at the minimum. The stat module returns all the details regarding the object specified in the path parameter. We need to store this in a register. Then using the return values and the ‘when’ condition statements, we can check various properties of the files.

Checking if the file exists or not exists
Let’s see a primary example of how to check if an object, file/directory, exists using the stat module.

In the following example, I am checking the status of ‘prompt.yaml’ file. I am storing the details of the stat module execution in a register named ‘file_details’. The module returns a lot of return values, and one of them is ‘exists’. It will return a boolean value, true or false, depending on the status of the file.
  1. - hosts: all  
  2.   tasks:  
  3.   - name: Ansible check file exists example.  
  4.     stat:  
  5.       path: /Users/mdtutorials2/Documents/Ansible/prompt.yaml  
  6.     register: file_details  
  7.   
  8.   - debug:  
  9.       msg: "The file or directory exists"  
  10.     when: file_details.stat.exists  
  11.     # check file not exist as below  
  12.     # when: not file_details.stat.exists   
The above example will give the output whether it is a file or directory or symlink. So how to check if a particular object exists and it is not a directory or symlink. We can use the ‘isreg‘ return value for checking if its a return value. It will be true if the path is a regular file.
  1. - debug:  
  2.     msg: "Ansible file exists example"  
  3.   when: file_details.stat.exists and file_details.stat.isreg  
Another way is to combine the ‘isdir‘ and ‘islnk‘ return values. The below task shows how it can be done.
  1. - debug:  
  2.      msg: "It is a file"  
  3.    when: file_details.stat.exists and file_details.stat.isdir == false and file_details.stat.islnk == false  
Checking if a directory exists
For checking if a particular object is a directory and also it exists you can combine the ‘exists’ and ‘isdir’ return values. The ‘isdir’ value will be true if the object is a directory and else false. For example, in the below case I have given the path for ‘test1’ object. If it is not a directory, the task will be skipped.
  1. - hosts: all  
  2.   tasks:  
  3.   - name: Ansible check directory exists example.  
  4.     stat:  
  5.       path: /Users/dnpmacpro/Documents/Ansible/test1  
  6.     register: files_to_delete  
  7.   
  8.   - debug:  
  9.       msg: "It is a directory"  
  10.     when: files_to_delete.stat.exists and files_to_delete.stat.isdir  
Supplement
FAQ - How can I run a ansible task only if a file or directory does NOT exist?

2020年2月11日 星期二

[ Python 常見問題 ] SQLAlchemy: how to filter date field?

Source From Here
Question
Here is model:
  1. class User(Base):  
  2.     ...  
  3.     birthday = Column(Date, index=True)   #in database it's like '1987-01-17'  
  4.     ...  
want to filter between two dates, for example to choose all users in interval 18-30 years. How to implement it with SQLAlchemy?

I think of:
  1. query = DBSession.query(User).filter(  
  2.     and_(User.birthday >= '1988-01-17', User.birthday <= '1985-01-17')  
  3. )   
  4.   
  5. # means age >= 24 and age <= 27  
I know this is not correct, but how to do correct?

How-To
In fact, your query is right except for the typo: your filter is excluding all records: you should change the <= for >= and vice versa:
  1. qry = DBSession.query(User).filter(  
  2.         and_(User.birthday <= '1988-01-17', User.birthday >= '1985-01-17'))  
  3. # or same:  
  4. qry = DBSession.query(User).filter(User.birthday <= '1988-01-17').\  
  5.         filter(User.birthday >= '1985-01-17')  
Also you can use between:
  1. qry = DBSession.query(User).filter(User.birthday.between('1985-01-17''1988-01-17'))  

2020年2月10日 星期一

[ 常見問題 ] How do I get the name of the pipeline from inside the jenkinsfile

Source From Here
Question
env.JOB_NAME Is the pipeline name suffixed with the branch name. (Check Using Environment Variables)

So env.JOB_NAME will be _

How can I just get the pipeline name and store it in a var in the environment{} block at the top of my jenkinsfile to use through the file?

I don't want to resort to scripted pipeline just the declarative.

How-To
According to Global Variable Reference, we have:
JOB_BASE_NAME
Short Name of the project of this build stripping off folder paths, such as "foo" for "bar/foo".

which can give you the name of pipeline.

2020年2月9日 星期日

[Linux 常見問題] How to export variables that are set, all at once?

Source From Here
Question
If I defined environment variable(s) in a file, how do I export these variables all at once?

How-To
Assume we have a configuration file .env:
  1. MY_NAME='John'  
  2. MY_AGE='39'  
Then we can test it by test.sh:
  1. #!/bin/sh  
  2. echo -e "===== Setting from .env ====="  
  3. cat .env  
  4.   
  5. echo -e "\n===== My setting ====="  
  6. env | grep 'MY'  
  7.   
  8. source ./.env  
  9. echo -e "\n===== After .env, My setting ====="  
  10. env | grep 'MY'  
The execution result:
# ./test.sh
===== Setting from .env =====
MY_NAME='John'
MY_AGE='39'

===== My setting =====

===== After .env, My setting =====

Run the following command, before setting the variables:
From man page:
-a
When this option is on, the export attribute shall be set for each variable to which an assignment is performed;

To turn this option off, run set +a afterwards. Let's update test.sh:
  1. #!/bin/sh  
  2. echo -e "===== Setting from .env ====="  
  3. cat .env  
  4.   
  5. echo -e "\n===== My setting ====="  
  6. env | grep 'MY'  
  7. set -a  
  8. source ./.env  
  9. set +a  
  10. echo -e "\n===== After .env, My setting ====="  
  11. env | grep 'MY'  
This time, the execution will look like:
# ./test.sh
===== Setting from .env =====
MY_NAME='John'
MY_AGE='39'

===== My setting =====

===== After .env, My setting =====
MY_AGE=39
MY_NAME=John


[NodeJS 文章收集] NodeJS accessing file with relative path

Source From  Here Question It seemed like a straight forward problem. But I am not able to crack this. Within  helper1.js  I would like to a...