技术诚可贵,生命价更高

技术值钱。因为,时间宝贵,生命宝贵。
一个小小的技术细节,如果不理解,不懂得,就会让人花了个把个小时去攻破。在个解决问题的过程之中,自己完全忽略外界,整个人被占有。一旦问题解决,自己马上就释放出来。这些宝贵的时间,就在一点一滴中度过了。
不但时间花费过去,整个人的健康也在其中被消耗掉了。干技术的,一旦技术一更新,又得撅着屁股学新的,学习的代价昂贵,耗费精力之大,所以不得不说,技术值钱,是有道理的。
然而,有没有更好的体现值钱的方法?
技术做得再好,代码写得再好,依然不能替代生活,怎么可以忘记,生活才是人生的主线?
技术付钱,生命更值钱。用有限的生命,去换取一朝一夕的技术,大体上,是不值得的。以此谋生,非要懂得平衡付出和收获不可。
话说回来,即使享受着深入练习、深入思考的快感,过去沉迷也是不好的。
所以,也要在方式、方法上多加分析。
  1. 磨刀不误砍柴工,善用工具。光用苦力,在技术上是无意义的。
  2. 少写,多思考,考虑得差不多了之后再写。多收集经验、有没有成熟模型?有没有同样的人遇到同样的问题?文档是怎么写的。这些都要考虑清楚。
  3. 遇到问题,不做过多纠结。超过1小时以上没有解决,一定是自己的方式错误。要先停下来,和练琴一样。
  4. 写东西之前,先列一个框架。写越大的项目,就越是需要架构能力和框架能力。有了架构,才有了方向。列好之后,分段、分时间解决。例如,有几个核心模块?又有多少子模块?每次的模块花久写?Priority 是什么?每个模块花多少时间?
  5. 要时刻关注原理,而不是表象的功能。
  6. 慢下来,不要急
以点,在解决技术问题时要时刻谨记。

Python 非常奇怪的代码现象

  1. #!/usr/bin/python
    
    
    import os
    from subprocess import *
    import sys
    
    import yaml
    def local_run(cmd):
        if cmd:
            p = Popen(cmd, shell=True, stdout=PIPE,stderr=PIPE)
            out,err = p.communicate()
        return(p.returncode, out, err)
    
    
    if __name__ == '__main__':
        rst = local_run("salt 'dev*' state.apply test  --out=yaml")
        data=yaml.load(rst[1])
        if rst[0] == 0:
            print([ data[k][cmd]['result'] for cmd in data[k].keys() for k in data.keys() ])
            print([ [data[k][cmd]['result'] for cmd in data[k].keys() ] for k in data.keys() ])
            # method 1
            #for k in data.keys():
            #    for cmd in data[k].keys():
            #        print data[k][cmd]['result']
            # method 2
        else:
            print rst[2]
            [ data[k][cmd]['result'] for cmd in data[k].keys() for k in data.keys() ]
            sys.exit(rst[0])
    
    

    Traceback (most recent call last):

      File “test.py”, line 20, in <module>

        print([ data[k][cmd][‘result’] for cmd in data[k].keys() for k in data.keys() ])

    NameError: name ‘k’ is not defined

    2. 将两个Expression 对掉,却能生效

    #!/usr/bin/python
    
    
    import os
    from subprocess import *
    import sys
    
    import yaml
    def local_run(cmd):
        if cmd:
            p = Popen(cmd, shell=True, stdout=PIPE,stderr=PIPE)
            out,err = p.communicate()
        return(p.returncode, out, err)
    
    
    if __name__ == '__main__':
        rst = local_run("salt 'dev*' state.apply test  --out=yaml")
        data=yaml.load(rst[1])
        if rst[0] == 0:
            print([ [data[k][cmd]['result'] for cmd in data[k].keys() ] for k in data.keys() ])
            print([ data[k][cmd]['result'] for cmd in data[k].keys() for k in data.keys() ])
            # method 1
            #for k in data.keys():
            #    for cmd in data[k].keys():
            #        print data[k][cmd]['result']
            # method 2
        else:
            print rst[2]
            [ data[k][cmd]['result'] for cmd in data[k].keys() for k in data.keys() ]
            sys.exit(rst[0])

    [[True], [True]]

    [True, True]

    奇怪。

Saltstack Set Roles with Pillar

Saltstack Set Roles with Pillar

Sometimes, we would like to set roles for our servers. This is can be done to define  /srv/pillar/top.sls.

for example

we have:

web_prod01

web_prod02

web_prod02

PART 1 working with pillar

  1. then we can define like this:
base:
   'web_prod*'
       - prod_role

 

2. and in the /srv/pillar/prod_role.sls

role: webserver

3. refresh the pillar file to all matched web_prod*minions

salt “web_prod*” saltutil.refresh_pillar

4. then test it

salt -I ‘role:webserver’ test.ping

additional we can set matched in state file:

base:
 'role:webserver':
   - match: pillar
   - state
   - state2

Reference links:

PART 2 another workaround with external nodegroups

include files in /etc/salt/master

# Include config from several files and directories:

include:

   – /opt/test/saltmgt/nodegroups.yaml

2. cat /opt/test/saltmgt/nodegroups.yaml

nodegroups:

   test1:

     – ‘*prod*’

 

more useful:

Working with Django Model

  1. Sub-queries used for query pk dee in  main talbe

Create your models here.
class projects(models.Model):
    #env = models.ForeignKey(deploy_env, on_delete=models.CASCADE)
    project_name = models.CharField(max_length=200)
    create_date = models.DateTimeField(default=0)
    owner = models.CharField(max_length=200)

    def __str__(self):
        return self.project_name
class AppEnv(models.Model):

    name = models.CharField(max_length=200, unique=True)
    def __str__(self):
        return self.name

class MGDeployEnv(models.Model):
    deploy_env = models.OneToOneField(minion_groups, on_delete=models.CASCADE)
    project_name = models.ForeignKey(projects, on_delete=models.CASCADE)
    env_name = models.ForeignKey(AppEnv, on_delete=models.CASCADE)
    pillar = models.TextField()
    state = models.TextField()
    current_version = models.CharField(max_length=200, blank=True)
    status = models.CharField(max_length=200, blank=True)
    comments = models.CharField(max_length=200, blank=True)
    update_on = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return str(self.deploy_env)
class tasks(models.Model): #question = models.ForeignKey(Question, on_delete=models.CASCADE) #project_name = models.ForeignKey(projects, on_delete=models.CASCADE) env = models.ForeignKey(deploy_env, on_delete=models.CASCADE) deploy_url = models.CharField(max_length=200) owner = models.CharField(max_length=200) date = models.DateTimeField(default=timezone.now) status = models.CharField(max_length=200, blank=True) result = models.TextField(blank=True) def __str__(self): return self.deploy_url

1.1 Command line tests:

>> env_id=deploy_env.objects.filter(project_name_id=5)
>>> qt=tasks.objects.filter(env_id__in=env_id)
>>> qt.values()

<QuerySet [{‘deploy_url’: ‘testurl’, ‘id’: 11, ‘env_id’: 12, ….. etc

class tasks(models.Model):
    #question = models.ForeignKey(Question, on_delete=models.CASCADE)
    #project_name = models.ForeignKey(projects, on_delete=models.CASCADE)
    #env = models.ForeignKey(deploy_env, on_delete=models.CASCADE)
    env = models.ForeignKey(MGDeployEnv, on_delete=models.CASCADE)
    deploy_url = models.CharField(max_length=200)
    owner = models.CharField(max_length=200)
    date = models.DateTimeField(default=timezone.now)
    status = models.CharField(max_length=200, blank=True)
    result = models.TextField(blank=True)

    def __str__(self):
        return self.deploy_url

More Model Query:

There two methods to get the searching result, for example.

1.2 Foward query (query by main set)

if we would like get the all deploy environment of PROD,  or deploy environment of Projects test, we can do following query:

by project

pj=projects.objects.get(project_name=‘abc’)

pj.mgdeployenv_set.all()

or shorter 

projects.objects.get(project_name=‘cpg’).mgdeployenv_set.all()

<QuerySet [<MGDeployEnv: cpg_cons_be>, <MGDeployEnv: cpg_cons_all>]>

by environments

env = AppEnv.objects.get(name=’cons’)

env.mgdeployenv_set.all()

QuerySet [<MGDeployEnv: abc_cons_be>, <MGDeployEnv: abc_cons_all>]>

1.3 BackForward query(query by sub set)

by project

MGDeployEnv.objects.filter(project_name__in=projects.objects.filter(project_name=‘abc’))

<QuerySet [<MGDeployEnv: abc_cons_be>, <MGDeployEnv: abc_cons_all>]>

by environments

MGDeployEnv.objects.filter(env_name__in=AppEnv.objects.filter(name=‘cons’))

<QuerySet [<MGDeployEnv: abc_cons_be>, <MGDeployEnv: abc_cons_all>]>

 

as you can see, this 4 queries are the same.

 

Reference Links:

http://scottlobdell.me/2015/01/sql-database-best-practices-django-orm/ 

 

2. Many to Many

https://docs.djangoproject.com/en/2.0/topics/db/examples/many_to_many/

Standalone script with Django

I am going to run external script by invoking Django module, db models etc.

Here the refernece link from django official docs.:

https://docs.djangoproject.com/en/2.0/topics/settings/

However, for the quote of website which is not working for me:

import django
from django.conf import settings
from myapp import myapp_defaults

settings.configure(default_settings=myapp_defaults, DEBUG=True)
django.setup()

# Now this script or any imported module can use any part of Django it needs.
from myapp import models

simply I am using following code instead:

#!/usr/bin/env python3

import os
import sys

project_path="/home/liboma/saltmgt/"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "saltmgt.saltmgt.settings")
sys.path.append(project_path)
#os.chdir(project_path)
if __name__ == '__main__':
    import django
    django.setup()
    from autocd.models import *
    <<your scripts>>

which is working fine for me.

Enjoy 🙂

other refernece links which helps:

  •  http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/
  • https://www.stavros.io/posts/standalone-django-scripts-definitive-guide/